aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Framework/Scenes/SOPVehicle.cs
blob: 8d11331740cff804ec8fe10c016f2a70416e3dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSimulator Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using OpenMetaverse;
using OpenSim.Framework;
using OpenSim.Region.PhysicsModules.SharedBase;
using System.Text;
using System.IO;
using System.Xml;
using OpenSim.Framework.Serialization;
using OpenSim.Framework.Serialization.External;
using OpenSim.Region.Framework.Scenes.Serialization;

namespace OpenSim.Region.Framework.Scenes
{
    public class SOPVehicle
    {
        public VehicleData vd;

        public Vehicle Type
        {
            get { return vd.m_type; }
        }

        public SOPVehicle()
        {
            vd = new VehicleData();
            ProcessTypeChange(Vehicle.TYPE_NONE); // is needed?
        }

        public void ProcessFloatVehicleParam(Vehicle pParam, float pValue)
        {
            float len;
            float timestep = 0.01f;
            switch (pParam)
            {
                case Vehicle.ANGULAR_DEFLECTION_EFFICIENCY:
                    if (pValue < 0f) pValue = 0f;
                    if (pValue > 1f) pValue = 1f;
                    vd.m_angularDeflectionEfficiency = pValue;
                    break;
                case Vehicle.ANGULAR_DEFLECTION_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_angularDeflectionTimescale = pValue;
                    break;
                case Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    else if (pValue > 120) pValue = 120;
                    vd.m_angularMotorDecayTimescale = pValue;
                    break;
                case Vehicle.ANGULAR_MOTOR_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_angularMotorTimescale = pValue;
                    break;
                case Vehicle.BANKING_EFFICIENCY:
                    if (pValue < -1f) pValue = -1f;
                    if (pValue > 1f) pValue = 1f;
                    vd.m_bankingEfficiency = pValue;
                    break;
                case Vehicle.BANKING_MIX:
                    if (pValue < 0f) pValue = 0f;
                    if (pValue > 1f) pValue = 1f;
                    vd.m_bankingMix = pValue;
                    break;
                case Vehicle.BANKING_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_bankingTimescale = pValue;
                    break;
                case Vehicle.BUOYANCY:
                    if (pValue < -1f) pValue = -1f;
                    if (pValue > 1f) pValue = 1f;
                    vd.m_VehicleBuoyancy = pValue;
                    break;
                case Vehicle.HOVER_EFFICIENCY:
                    if (pValue < 0f) pValue = 0f;
                    if (pValue > 1f) pValue = 1f;
                    vd.m_VhoverEfficiency = pValue;
                    break;
                case Vehicle.HOVER_HEIGHT:
                    vd.m_VhoverHeight = pValue;
                    break;
                case Vehicle.HOVER_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_VhoverTimescale = pValue;
                    break;
                case Vehicle.LINEAR_DEFLECTION_EFFICIENCY:
                    if (pValue < 0f) pValue = 0f;
                    if (pValue > 1f) pValue = 1f;
                    vd.m_linearDeflectionEfficiency = pValue;
                    break;
                case Vehicle.LINEAR_DEFLECTION_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_linearDeflectionTimescale = pValue;
                    break;
                case Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    else if (pValue > 120) pValue = 120;
                    vd.m_linearMotorDecayTimescale = pValue;
                    break;
                case Vehicle.LINEAR_MOTOR_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_linearMotorTimescale = pValue;
                    break;
                case Vehicle.VERTICAL_ATTRACTION_EFFICIENCY:
                    if (pValue < 0f) pValue = 0f;
                    if (pValue > 1f) pValue = 1f;
                    vd.m_verticalAttractionEfficiency = pValue;
                    break;
                case Vehicle.VERTICAL_ATTRACTION_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_verticalAttractionTimescale = pValue;
                    break;

                // These are vector properties but the engine lets you use a single float value to
                // set all of the components to the same value
                case Vehicle.ANGULAR_FRICTION_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_angularFrictionTimescale = new Vector3(pValue, pValue, pValue);
                    break;
                case Vehicle.ANGULAR_MOTOR_DIRECTION:
                    vd.m_angularMotorDirection = new Vector3(pValue, pValue, pValue);
                    len = vd.m_angularMotorDirection.Length();
                    if (len > 12.566f)
                        vd.m_angularMotorDirection *= (12.566f / len);
                    break;
                case Vehicle.LINEAR_FRICTION_TIMESCALE:
                    if (pValue < timestep) pValue = timestep;
                    vd.m_linearFrictionTimescale = new Vector3(pValue, pValue, pValue);
                    break;
                case Vehicle.LINEAR_MOTOR_DIRECTION:
                    vd.m_linearMotorDirection = new Vector3(pValue, pValue, pValue);
                    len = vd.m_linearMotorDirection.Length();
                    if (len > 30.0f)
                        vd.m_linearMotorDirection *= (30.0f / len);
                    break;
                case Vehicle.LINEAR_MOTOR_OFFSET:
                    vd.m_linearMotorOffset = new Vector3(pValue, pValue, pValue);
                    len = vd.m_linearMotorOffset.Length();
                    if (len > 100.0f)
                        vd.m_linearMotorOffset *= (100.0f / len);
                    break;
            }
        }//end ProcessFloatVehicleParam

        public void ProcessVectorVehicleParam(Vehicle pParam, Vector3 pValue)
        {
            float len;
            float timestep = 0.01f;
            switch (pParam)
            {
                case Vehicle.ANGULAR_FRICTION_TIMESCALE:
                    if (pValue.X < timestep) pValue.X = timestep;
                    if (pValue.Y < timestep) pValue.Y = timestep;
                    if (pValue.Z < timestep) pValue.Z = timestep;

                    vd.m_angularFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
                    break;
                case Vehicle.ANGULAR_MOTOR_DIRECTION:
                    vd.m_angularMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
                    // Limit requested angular speed to 2 rps= 4 pi rads/sec
                    len = vd.m_angularMotorDirection.Length();
                    if (len > 12.566f)
                        vd.m_angularMotorDirection *= (12.566f / len);
                    break;
                case Vehicle.LINEAR_FRICTION_TIMESCALE:
                    if (pValue.X < timestep) pValue.X = timestep;
                    if (pValue.Y < timestep) pValue.Y = timestep;
                    if (pValue.Z < timestep) pValue.Z = timestep;
                    vd.m_linearFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
                    break;
                case Vehicle.LINEAR_MOTOR_DIRECTION:
                    vd.m_linearMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
                    len = vd.m_linearMotorDirection.Length();
                    if (len > 30.0f)
                        vd.m_linearMotorDirection *= (30.0f / len);
                    break;
                case Vehicle.LINEAR_MOTOR_OFFSET:
                    vd.m_linearMotorOffset = new Vector3(pValue.X, pValue.Y, pValue.Z);
                    len = vd.m_linearMotorOffset.Length();
                    if (len > 100.0f)
                        vd.m_linearMotorOffset *= (100.0f / len);
                    break;
            }
        }//end ProcessVectorVehicleParam

        public void ProcessRotationVehicleParam(Vehicle pParam, Quaternion pValue)
        {
            switch (pParam)
            {
                case Vehicle.REFERENCE_FRAME:
                    vd.m_referenceFrame = pValue;
                    break;
            }
        }//end ProcessRotationVehicleParam

        public void ProcessVehicleFlags(int pParam, bool remove)
        {
            if (remove)
            {
                vd.m_flags &= ~((VehicleFlag)pParam);
            }
            else
            {
                vd.m_flags |= (VehicleFlag)pParam;
            }
        }//end ProcessVehicleFlags

        public void ProcessTypeChange(Vehicle pType)
        {
            vd.m_linearMotorDirection = Vector3.Zero;
            vd.m_angularMotorDirection = Vector3.Zero;
            vd.m_linearMotorOffset = Vector3.Zero;
            vd.m_referenceFrame = Quaternion.Identity;

            // Set Defaults For Type
            vd.m_type = pType;
            switch (pType)
            {
                case Vehicle.TYPE_NONE:
                    vd.m_linearFrictionTimescale = new Vector3(1000, 1000, 1000);
                    vd.m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
                    vd.m_linearMotorTimescale = 1000;
                    vd.m_linearMotorDecayTimescale = 120;
                    vd.m_angularMotorTimescale = 1000;
                    vd.m_angularMotorDecayTimescale = 1000;
                    vd.m_VhoverHeight = 0;
                    vd.m_VhoverEfficiency = 1;
                    vd.m_VhoverTimescale = 1000;
                    vd.m_VehicleBuoyancy = 0;
                    vd.m_linearDeflectionEfficiency = 0;
                    vd.m_linearDeflectionTimescale = 1000;
                    vd.m_angularDeflectionEfficiency = 0;
                    vd.m_angularDeflectionTimescale = 1000;
                    vd.m_bankingEfficiency = 0;
                    vd.m_bankingMix = 1;
                    vd.m_bankingTimescale = 1000;
                    vd.m_verticalAttractionEfficiency = 0;
                    vd.m_verticalAttractionTimescale = 1000;

                    vd.m_flags = (VehicleFlag)0;
                    break;

                case Vehicle.TYPE_SLED:
                    vd.m_linearFrictionTimescale = new Vector3(30, 1, 1000);
                    vd.m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
                    vd.m_linearMotorTimescale = 1000;
                    vd.m_linearMotorDecayTimescale = 120;
                    vd.m_angularMotorTimescale = 1000;
                    vd.m_angularMotorDecayTimescale = 120;
                    vd.m_VhoverHeight = 0;
                    vd.m_VhoverEfficiency = 1;
                    vd.m_VhoverTimescale = 10;
                    vd.m_VehicleBuoyancy = 0;
                    vd.m_linearDeflectionEfficiency = 1;
                    vd.m_linearDeflectionTimescale = 1;
                    vd.m_angularDeflectionEfficiency = 0;
                    vd.m_angularDeflectionTimescale = 1000;
                    vd.m_bankingEfficiency = 0;
                    vd.m_bankingMix = 1;
                    vd.m_bankingTimescale = 10;
                    vd.m_flags &=
                         ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
                           VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY);
                    vd.m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.LIMIT_MOTOR_UP);
                    break;
                case Vehicle.TYPE_CAR:
                    vd.m_linearFrictionTimescale = new Vector3(100, 2, 1000);
                    vd.m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
                    vd.m_linearMotorTimescale = 1;
                    vd.m_linearMotorDecayTimescale = 60;
                    vd.m_angularMotorTimescale = 1;
                    vd.m_angularMotorDecayTimescale = 0.8f;
                    vd.m_VhoverHeight = 0;
                    vd.m_VhoverEfficiency = 0;
                    vd.m_VhoverTimescale = 1000;
                    vd.m_VehicleBuoyancy = 0;
                    vd.m_linearDeflectionEfficiency = 1;
                    vd.m_linearDeflectionTimescale = 2;
                    vd.m_angularDeflectionEfficiency = 0;
                    vd.m_angularDeflectionTimescale = 10;
                    vd.m_verticalAttractionEfficiency = 1f;
                    vd.m_verticalAttractionTimescale = 10f;
                    vd.m_bankingEfficiency = -0.2f;
                    vd.m_bankingMix = 1;
                    vd.m_bankingTimescale = 1;
                    vd.m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT);
                    vd.m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY |
                                VehicleFlag.LIMIT_MOTOR_UP | VehicleFlag.HOVER_UP_ONLY);
                    break;
                case Vehicle.TYPE_BOAT:
                    vd.m_linearFrictionTimescale = new Vector3(10, 3, 2);
                    vd.m_angularFrictionTimescale = new Vector3(10, 10, 10);
                    vd.m_linearMotorTimescale = 5;
                    vd.m_linearMotorDecayTimescale = 60;
                    vd.m_angularMotorTimescale = 4;
                    vd.m_angularMotorDecayTimescale = 4;
                    vd.m_VhoverHeight = 0;
                    vd.m_VhoverEfficiency = 0.5f;
                    vd.m_VhoverTimescale = 2;
                    vd.m_VehicleBuoyancy = 1;
                    vd.m_linearDeflectionEfficiency = 0.5f;
                    vd.m_linearDeflectionTimescale = 3;
                    vd.m_angularDeflectionEfficiency = 0.5f;
                    vd.m_angularDeflectionTimescale = 5;
                    vd.m_verticalAttractionEfficiency = 0.5f;
                    vd.m_verticalAttractionTimescale = 5f;
                    vd.m_bankingEfficiency = -0.3f;
                    vd.m_bankingMix = 0.8f;
                    vd.m_bankingTimescale = 1;
                    vd.m_flags &= ~(VehicleFlag.HOVER_TERRAIN_ONLY |
                            VehicleFlag.HOVER_GLOBAL_HEIGHT |
                            VehicleFlag.HOVER_UP_ONLY |
                            VehicleFlag.LIMIT_ROLL_ONLY);
                    vd.m_flags |= (VehicleFlag.NO_DEFLECTION_UP |
                                VehicleFlag.LIMIT_MOTOR_UP |
                                VehicleFlag.HOVER_WATER_ONLY);
                    break;
                case Vehicle.TYPE_AIRPLANE:
                    vd.m_linearFrictionTimescale = new Vector3(200, 10, 5);
                    vd.m_angularFrictionTimescale = new Vector3(20, 20, 20);
                    vd.m_linearMotorTimescale = 2;
                    vd.m_linearMotorDecayTimescale = 60;
                    vd.m_angularMotorTimescale = 4;
                    vd.m_angularMotorDecayTimescale = 8;
                    vd.m_VhoverHeight = 0;
                    vd.m_VhoverEfficiency = 0.5f;
                    vd.m_VhoverTimescale = 1000;
                    vd.m_VehicleBuoyancy = 0;
                    vd.m_linearDeflectionEfficiency = 0.5f;
                    vd.m_linearDeflectionTimescale = 0.5f;
                    vd.m_angularDeflectionEfficiency = 1;
                    vd.m_angularDeflectionTimescale = 2;
                    vd.m_verticalAttractionEfficiency = 0.9f;
                    vd.m_verticalAttractionTimescale = 2f;
                    vd.m_bankingEfficiency = 1;
                    vd.m_bankingMix = 0.7f;
                    vd.m_bankingTimescale = 2;
                    vd.m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY |
                        VehicleFlag.HOVER_TERRAIN_ONLY |
                        VehicleFlag.HOVER_GLOBAL_HEIGHT |
                        VehicleFlag.HOVER_UP_ONLY |
                        VehicleFlag.NO_DEFLECTION_UP |
                        VehicleFlag.LIMIT_MOTOR_UP);
                    vd.m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY);
                    break;
                case Vehicle.TYPE_BALLOON:
                    vd.m_linearFrictionTimescale = new Vector3(5, 5, 5);
                    vd.m_angularFrictionTimescale = new Vector3(10, 10, 10);
                    vd.m_linearMotorTimescale = 5;
                    vd.m_linearMotorDecayTimescale = 60;
                    vd.m_angularMotorTimescale = 6;
                    vd.m_angularMotorDecayTimescale = 10;
                    vd.m_VhoverHeight = 5;
                    vd.m_VhoverEfficiency = 0.8f;
                    vd.m_VhoverTimescale = 10;
                    vd.m_VehicleBuoyancy = 1;
                    vd.m_linearDeflectionEfficiency = 0;
                    vd.m_linearDeflectionTimescale = 5;
                    vd.m_angularDeflectionEfficiency = 0;
                    vd.m_angularDeflectionTimescale = 5;
                    vd.m_verticalAttractionEfficiency = 0f;
                    vd.m_verticalAttractionTimescale = 1000f;
                    vd.m_bankingEfficiency = 0;
                    vd.m_bankingMix = 0.7f;
                    vd.m_bankingTimescale = 5;
                    vd.m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY |
                        VehicleFlag.HOVER_TERRAIN_ONLY |
                        VehicleFlag.HOVER_UP_ONLY |
                        VehicleFlag.NO_DEFLECTION_UP |
                        VehicleFlag.LIMIT_MOTOR_UP);
                    vd.m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY |
                        VehicleFlag.HOVER_GLOBAL_HEIGHT);
                    break;
            }
        }
        public void SetVehicle(PhysicsActor ph)
        {
            if (ph == null)
                return;
            ph.SetVehicle(vd);
        }

        public bool CameraDecoupled
        {
            get
            {
                if((vd.m_flags & VehicleFlag.CAMERA_DECOUPLED) != 0)
                    return true;
                return false;
            }
        }

        private XmlTextWriter writer;

        private void XWint(string name, int i)
        {
            writer.WriteElementString(name, i.ToString());
        }

        private void XWfloat(string name, float f)
        {
            writer.WriteElementString(name, f.ToString(Utils.EnUsCulture));
        }

        private void XWVector(string name, Vector3 vec)
        {
            writer.WriteStartElement(name);
            writer.WriteElementString("X", vec.X.ToString(Utils.EnUsCulture));
            writer.WriteElementString("Y", vec.Y.ToString(Utils.EnUsCulture));
            writer.WriteElementString("Z", vec.Z.ToString(Utils.EnUsCulture));
            writer.WriteEndElement();
        }

        private void XWQuat(string name, Quaternion quat)
        {
            writer.WriteStartElement(name);
            writer.WriteElementString("X", quat.X.ToString(Utils.EnUsCulture));
            writer.WriteElementString("Y", quat.Y.ToString(Utils.EnUsCulture));
            writer.WriteElementString("Z", quat.Z.ToString(Utils.EnUsCulture));
            writer.WriteElementString("W", quat.W.ToString(Utils.EnUsCulture));
            writer.WriteEndElement();
        }

        public void ToXml2(XmlTextWriter twriter)
        {
            writer = twriter;
            writer.WriteStartElement("Vehicle");

            XWint("TYPE", (int)vd.m_type);
            XWint("FLAGS", (int)vd.m_flags);

            // Linear properties
            XWVector("LMDIR", vd.m_linearMotorDirection);
            XWVector("LMFTIME", vd.m_linearFrictionTimescale);
            XWfloat("LMDTIME", vd.m_linearMotorDecayTimescale);
            XWfloat("LMTIME", vd.m_linearMotorTimescale);
            XWVector("LMOFF", vd.m_linearMotorOffset);

            //Angular properties
            XWVector("AMDIR", vd.m_angularMotorDirection);
            XWfloat("AMTIME", vd.m_angularMotorTimescale);
            XWfloat("AMDTIME", vd.m_angularMotorDecayTimescale);
            XWVector("AMFTIME", vd.m_angularFrictionTimescale);

            //Deflection properties
            XWfloat("ADEFF", vd.m_angularDeflectionEfficiency);
            XWfloat("ADTIME", vd.m_angularDeflectionTimescale);
            XWfloat("LDEFF", vd.m_linearDeflectionEfficiency);
            XWfloat("LDTIME", vd.m_linearDeflectionTimescale);

            //Banking properties
            XWfloat("BEFF", vd.m_bankingEfficiency);
            XWfloat("BMIX", vd.m_bankingMix);
            XWfloat("BTIME", vd.m_bankingTimescale);

            //Hover and Buoyancy properties
            XWfloat("HHEI", vd.m_VhoverHeight);
            XWfloat("HEFF", vd.m_VhoverEfficiency);
            XWfloat("HTIME", vd.m_VhoverTimescale);
            XWfloat("VBUO", vd.m_VehicleBuoyancy);

            //Attractor properties
            XWfloat("VAEFF", vd.m_verticalAttractionEfficiency);
            XWfloat("VATIME", vd.m_verticalAttractionTimescale);

            XWQuat("REF_FRAME", vd.m_referenceFrame);

            writer.WriteEndElement();
            writer = null;
        }



        XmlReader reader;

        private int XRint()
        {
            return reader.ReadElementContentAsInt();
        }

        private float XRfloat()
        {
            return reader.ReadElementContentAsFloat();
        }

        public Vector3 XRvector()
        {
            Vector3 vec;
            reader.ReadStartElement();
            vec.X = reader.ReadElementContentAsFloat();
            vec.Y = reader.ReadElementContentAsFloat();
            vec.Z = reader.ReadElementContentAsFloat();
            reader.ReadEndElement();
            return vec;
        }

        public Quaternion XRquat()
        {
            Quaternion q;
            reader.ReadStartElement();
            q.X = reader.ReadElementContentAsFloat();
            q.Y = reader.ReadElementContentAsFloat();
            q.Z = reader.ReadElementContentAsFloat();
            q.W = reader.ReadElementContentAsFloat();
            reader.ReadEndElement();
            return q;
        }

        public static bool EReadProcessors(
            Dictionary<string, Action> processors,
            XmlReader xtr)
        {
            bool errors = false;

            string nodeName = string.Empty;
            while (xtr.NodeType != XmlNodeType.EndElement)
            {
                nodeName = xtr.Name;

                // m_log.DebugFormat("[ExternalRepresentationUtils]: Processing: {0}", nodeName);

                Action p = null;
                if (processors.TryGetValue(xtr.Name, out p))
                {
                    // m_log.DebugFormat("[ExternalRepresentationUtils]: Found {0} processor, nodeName);

                    try
                    {
                        p();
                    }
                    catch
                    {
                        errors = true;
                        if (xtr.NodeType == XmlNodeType.EndElement)
                            xtr.Read();
                    }
                }
                else
                {
                    // m_log.DebugFormat("[LandDataSerializer]: caught unknown element {0}", nodeName);
                    xtr.ReadOuterXml(); // ignore
                }
            }

            return errors;
        }


        public string ToXml2()
        {
            using (StringWriter sw = new StringWriter())
            {
                using (XmlTextWriter xwriter = new XmlTextWriter(sw))
                {
                    ToXml2(xwriter);
                }

                return sw.ToString();
            }
        }

        public static SOPVehicle FromXml2(string text)
        {
            if (text == String.Empty)
                return null;

            UTF8Encoding enc = new UTF8Encoding();
            MemoryStream ms = new MemoryStream(enc.GetBytes(text));
            XmlTextReader xreader = new XmlTextReader(ms);

            SOPVehicle v = new SOPVehicle();
            bool error;

            v.FromXml2(xreader, out error);

            xreader.Close();

            if (error)
            {
                v = null;
                return null;
            }
            return v;
        }

        public static SOPVehicle FromXml2(XmlReader reader)
        {
            SOPVehicle vehicle = new SOPVehicle();

            bool errors = false;

            vehicle.FromXml2(reader, out errors);
            if (errors)
                return null;

            return vehicle;
        }

        private void FromXml2(XmlReader _reader, out bool errors)
        {
            errors = false;
            reader = _reader;

            Dictionary<string, Action> m_VehicleXmlProcessors
            = new Dictionary<string, Action>();

            m_VehicleXmlProcessors.Add("TYPE", ProcessXR_type);
            m_VehicleXmlProcessors.Add("FLAGS", ProcessXR_flags);

            // Linear properties
            m_VehicleXmlProcessors.Add("LMDIR", ProcessXR_linearMotorDirection);
            m_VehicleXmlProcessors.Add("LMFTIME", ProcessXR_linearFrictionTimescale);
            m_VehicleXmlProcessors.Add("LMDTIME", ProcessXR_linearMotorDecayTimescale);
            m_VehicleXmlProcessors.Add("LMTIME", ProcessXR_linearMotorTimescale);
            m_VehicleXmlProcessors.Add("LMOFF", ProcessXR_linearMotorOffset);

            //Angular properties
            m_VehicleXmlProcessors.Add("AMDIR", ProcessXR_angularMotorDirection);
            m_VehicleXmlProcessors.Add("AMTIME", ProcessXR_angularMotorTimescale);
            m_VehicleXmlProcessors.Add("AMDTIME", ProcessXR_angularMotorDecayTimescale);
            m_VehicleXmlProcessors.Add("AMFTIME", ProcessXR_angularFrictionTimescale);

            //Deflection properties
            m_VehicleXmlProcessors.Add("ADEFF", ProcessXR_angularDeflectionEfficiency);
            m_VehicleXmlProcessors.Add("ADTIME", ProcessXR_angularDeflectionTimescale);
            m_VehicleXmlProcessors.Add("LDEFF", ProcessXR_linearDeflectionEfficiency);
            m_VehicleXmlProcessors.Add("LDTIME", ProcessXR_linearDeflectionTimescale);

            //Banking properties
            m_VehicleXmlProcessors.Add("BEFF", ProcessXR_bankingEfficiency);
            m_VehicleXmlProcessors.Add("BMIX", ProcessXR_bankingMix);
            m_VehicleXmlProcessors.Add("BTIME", ProcessXR_bankingTimescale);

            //Hover and Buoyancy properties
            m_VehicleXmlProcessors.Add("HHEI", ProcessXR_VhoverHeight);
            m_VehicleXmlProcessors.Add("HEFF", ProcessXR_VhoverEfficiency);
            m_VehicleXmlProcessors.Add("HTIME", ProcessXR_VhoverTimescale);

            m_VehicleXmlProcessors.Add("VBUO", ProcessXR_VehicleBuoyancy);

            //Attractor properties
            m_VehicleXmlProcessors.Add("VAEFF", ProcessXR_verticalAttractionEfficiency);
            m_VehicleXmlProcessors.Add("VATIME", ProcessXR_verticalAttractionTimescale);

            m_VehicleXmlProcessors.Add("REF_FRAME", ProcessXR_referenceFrame);

            vd = new VehicleData();

            reader.ReadStartElement("Vehicle", String.Empty);

            errors = EReadProcessors(
                m_VehicleXmlProcessors,
                reader);

            reader.ReadEndElement();
            reader = null;
        }

        private void ProcessXR_type()
        {
            vd.m_type = (Vehicle)XRint();
        }
        private void ProcessXR_flags()
        {
            vd.m_flags = (VehicleFlag)XRint();
        }
        // Linear properties
        private void ProcessXR_linearMotorDirection()
        {
            vd.m_linearMotorDirection = XRvector();
        }

        private void ProcessXR_linearFrictionTimescale()
        {
            vd.m_linearFrictionTimescale = XRvector();
        }

        private void ProcessXR_linearMotorDecayTimescale()
        {
            vd.m_linearMotorDecayTimescale = XRfloat();
        }
        private void ProcessXR_linearMotorTimescale()
        {
            vd.m_linearMotorTimescale = XRfloat();
        }
        private void ProcessXR_linearMotorOffset()
        {
            vd.m_linearMotorOffset = XRvector();
        }


        //Angular properties
        private void ProcessXR_angularMotorDirection()
        {
            vd.m_angularMotorDirection = XRvector();
        }
        private void ProcessXR_angularMotorTimescale()
        {
            vd.m_angularMotorTimescale = XRfloat();
        }
        private void ProcessXR_angularMotorDecayTimescale()
        {
            vd.m_angularMotorDecayTimescale = XRfloat();
        }
        private void ProcessXR_angularFrictionTimescale()
        {
            vd.m_angularFrictionTimescale = XRvector();
        }

        //Deflection properties
        private void ProcessXR_angularDeflectionEfficiency()
        {
            vd.m_angularDeflectionEfficiency = XRfloat();
        }
        private void ProcessXR_angularDeflectionTimescale()
        {
            vd.m_angularDeflectionTimescale = XRfloat();
        }
        private void ProcessXR_linearDeflectionEfficiency()
        {
            vd.m_linearDeflectionEfficiency = XRfloat();
        }
        private void ProcessXR_linearDeflectionTimescale()
        {
            vd.m_linearDeflectionTimescale = XRfloat();
        }

        //Banking properties
        private void ProcessXR_bankingEfficiency()
        {
            vd.m_bankingEfficiency = XRfloat();
        }
        private void ProcessXR_bankingMix()
        {
            vd.m_bankingMix = XRfloat();
        }
        private void ProcessXR_bankingTimescale()
        {
            vd.m_bankingTimescale = XRfloat();
        }

        //Hover and Buoyancy properties
        private void ProcessXR_VhoverHeight()
        {
            vd.m_VhoverHeight = XRfloat();
        }
        private void ProcessXR_VhoverEfficiency()
        {
            vd.m_VhoverEfficiency = XRfloat();
        }
        private void ProcessXR_VhoverTimescale()
        {
            vd.m_VhoverTimescale = XRfloat();
        }

        private void ProcessXR_VehicleBuoyancy()
        {
            vd.m_VehicleBuoyancy = XRfloat();
        }

        //Attractor properties
        private void ProcessXR_verticalAttractionEfficiency()
        {
            vd.m_verticalAttractionEfficiency = XRfloat();
        }
        private void ProcessXR_verticalAttractionTimescale()
        {
            vd.m_verticalAttractionTimescale = XRfloat();
        }

        private void ProcessXR_referenceFrame()
        {
            vd.m_referenceFrame = XRquat();
        }
    }
}