aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/BulletSPlugin/BSScene.cs
blob: 79ac5a5ba0c4b7619dccdfbe30bc79eee7ba0a31 (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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
/*
 * 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 copyrightD
 *       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 System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using OpenSim.Framework;
using OpenSim.Region.Framework;
using OpenSim.Region.CoreModules;
using Logging = OpenSim.Region.CoreModules.Framework.Statistics.Logging;
using OpenSim.Region.Physics.Manager;
using Nini.Config;
using log4net;
using OpenMetaverse;

namespace OpenSim.Region.Physics.BulletSPlugin
{
public sealed class BSScene : PhysicsScene, IPhysicsParameters
{
    internal static readonly ILog m_log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    internal static readonly string LogHeader = "[BULLETS SCENE]";

    // The name of the region we're working for.
    public string RegionName { get; private set; }

    public string BulletSimVersion = "?";

    // The handle to the underlying managed or unmanaged version of Bullet being used.
    public string BulletEngineName { get; private set; }
    public BSAPITemplate PE;

    // If the physics engine is running on a separate thread
    public Thread m_physicsThread;

    public Dictionary<uint, BSPhysObject> PhysObjects;
    public BSShapeCollection Shapes;

    // Keeping track of the objects with collisions so we can report begin and end of a collision
    public HashSet<BSPhysObject> ObjectsWithCollisions = new HashSet<BSPhysObject>();
    public HashSet<BSPhysObject> ObjectsWithNoMoreCollisions = new HashSet<BSPhysObject>();

    // All the collision processing is protected with this lock object
    public Object CollisionLock = new Object();

    // Properties are updated here
    public Object UpdateLock = new Object();
    public HashSet<BSPhysObject> ObjectsWithUpdates = new HashSet<BSPhysObject>();

    // Keep track of all the avatars so we can send them a collision event
    //    every tick so OpenSim will update its animation.
    private HashSet<BSPhysObject> m_avatars = new HashSet<BSPhysObject>();

    // let my minuions use my logger
    public ILog Logger { get { return m_log; } }

    public IMesher mesher;
    public uint WorldID { get; private set; }
    public BulletWorld World { get; private set; }

    // All the constraints that have been allocated in this instance.
    public BSConstraintCollection Constraints { get; private set; }

    // Simulation parameters
    internal float m_physicsStepTime;   // if running independently, the interval simulated by default

    internal int m_maxSubSteps;
    internal float m_fixedTimeStep;

    internal float m_simulatedTime;     // the time simulated previously. Used for physics framerate calc.

    internal long m_simulationStep = 0; // The current simulation step.
    public long SimulationStep { get { return m_simulationStep; } }
    // A number to use for SimulationStep that is probably not any step value
    // Used by the collision code (which remembers the step when a collision happens) to remember not any simulation step.
    public static long NotASimulationStep = -1234;

    internal float LastTimeStep { get; private set; }   // The simulation time from the last invocation of Simulate()

    internal float NominalFrameRate { get; set; }       // Parameterized ideal frame rate that simulation is scaled to

    // Physical objects can register for prestep or poststep events
    public delegate void PreStepAction(float timeStep);
    public delegate void PostStepAction(float timeStep);
    public event PreStepAction BeforeStep;
    public event PostStepAction AfterStep;

    // A value of the time 'now' so all the collision and update routines do not have to get their own
    // Set to 'now' just before all the prims and actors are called for collisions and updates
    public int SimulationNowTime { get; private set; }

    // True if initialized and ready to do simulation steps
    private bool m_initialized = false;

    // Flag which is true when processing taints.
    // Not guaranteed to be correct all the time (don't depend on this) but good for debugging.
    public bool InTaintTime { get; private set; }

    // Pinned memory used to pass step information between managed and unmanaged
    internal int m_maxCollisionsPerFrame;
    internal CollisionDesc[] m_collisionArray;

    internal int m_maxUpdatesPerFrame;
    internal EntityProperties[] m_updateArray;

    public const uint TERRAIN_ID = 0;       // OpenSim senses terrain with a localID of zero
    public const uint GROUNDPLANE_ID = 1;
    public const uint CHILDTERRAIN_ID = 2;  // Terrain allocated based on our mega-prim childre start here

    public float SimpleWaterLevel { get; set; }
    public BSTerrainManager TerrainManager { get; private set; }

    public ConfigurationParameters Params
    {
        get { return UnmanagedParams[0]; }
    }
    public Vector3 DefaultGravity
    {
        get { return new Vector3(0f, 0f, Params.gravity); }
    }
    // Just the Z value of the gravity
    public float DefaultGravityZ
    {
        get { return Params.gravity; }
    }

    // When functions in the unmanaged code must be called, it is only
    //   done at a known time just before the simulation step. The taint
    //   system saves all these function calls and executes them in
    //   order before the simulation.
    public delegate void TaintCallback();
    private struct TaintCallbackEntry
    {
        public String ident;
        public TaintCallback callback;
        public TaintCallbackEntry(string i, TaintCallback c)
        {
            ident = i;
            callback = c;
        }
    }
    private Object _taintLock = new Object();   // lock for using the next object
    private List<TaintCallbackEntry> _taintOperations;
    private Dictionary<string, TaintCallbackEntry> _postTaintOperations;
    private List<TaintCallbackEntry> _postStepOperations;

    // A pointer to an instance if this structure is passed to the C++ code
    // Used to pass basic configuration values to the unmanaged code.
    internal ConfigurationParameters[] UnmanagedParams;

    // Sometimes you just have to log everything.
    public Logging.LogWriter PhysicsLogging;
    private bool m_physicsLoggingEnabled;
    private string m_physicsLoggingDir;
    private string m_physicsLoggingPrefix;
    private int m_physicsLoggingFileMinutes;
    private bool m_physicsLoggingDoFlush;
    private bool m_physicsPhysicalDumpEnabled;
    public int PhysicsMetricDumpFrames { get; set; }
    // 'true' of the vehicle code is to log lots of details
    public bool VehicleLoggingEnabled { get; private set; }
    public bool VehiclePhysicalLoggingEnabled { get; private set; }

    #region Construction and Initialization
    public BSScene(string engineType, string identifier)
    {
        m_initialized = false;

        // The name of the region we're working for is passed to us. Keep for identification.
        RegionName = identifier;

        // Set identifying variables in the PhysicsScene interface.
        EngineType = engineType;
        Name = EngineType + "/" + RegionName;
    }

    public override void Initialise(IMesher meshmerizer, IConfigSource config)
    {
        mesher = meshmerizer;
        _taintOperations = new List<TaintCallbackEntry>();
        _postTaintOperations = new Dictionary<string, TaintCallbackEntry>();
        _postStepOperations = new List<TaintCallbackEntry>();
        PhysObjects = new Dictionary<uint, BSPhysObject>();
        Shapes = new BSShapeCollection(this);

        m_simulatedTime = 0f;
        LastTimeStep = 0.1f;

        // Allocate pinned memory to pass parameters.
        UnmanagedParams = new ConfigurationParameters[1];

        // Set default values for physics parameters plus any overrides from the ini file
        GetInitialParameterValues(config);

        // Get the connection to the physics engine (could be native or one of many DLLs)
        PE = SelectUnderlyingBulletEngine(BulletEngineName);

        // Enable very detailed logging.
        // By creating an empty logger when not logging, the log message invocation code
        //     can be left in and every call doesn't have to check for null.
        if (m_physicsLoggingEnabled)
        {
            PhysicsLogging = new Logging.LogWriter(m_physicsLoggingDir, m_physicsLoggingPrefix, m_physicsLoggingFileMinutes, m_physicsLoggingDoFlush);
            PhysicsLogging.ErrorLogger = m_log; // for DEBUG. Let's the logger output its own error messages.
        }
        else
        {
            PhysicsLogging = new Logging.LogWriter();
        }

        // Allocate memory for returning of the updates and collisions from the physics engine
        m_collisionArray = new CollisionDesc[m_maxCollisionsPerFrame];
        m_updateArray = new EntityProperties[m_maxUpdatesPerFrame];

        // The bounding box for the simulated world. The origin is 0,0,0 unless we're
        //    a child in a mega-region.
        // Bullet actually doesn't care about the extents of the simulated
        //    area. It tracks active objects no matter where they are.
        Vector3 worldExtent = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight);

        World = PE.Initialize(worldExtent, Params, m_maxCollisionsPerFrame, ref m_collisionArray, m_maxUpdatesPerFrame, ref m_updateArray);

        Constraints = new BSConstraintCollection(World);

        TerrainManager = new BSTerrainManager(this);
        TerrainManager.CreateInitialGroundPlaneAndTerrain();

        // Put some informational messages into the log file.
        m_log.InfoFormat("{0} Linksets implemented with {1}", LogHeader, (BSLinkset.LinksetImplementation)BSParam.LinksetImplementation);

        InTaintTime = false;
        m_initialized = true;

        // If the physics engine runs on its own thread, start same.
        if (BSParam.UseSeparatePhysicsThread)
        {
            // The physics simulation should happen independently of the heartbeat loop
            m_physicsThread = new Thread(BulletSPluginPhysicsThread);
            m_physicsThread.Name = BulletEngineName;
            m_physicsThread.Start();
        }
    }

    // All default parameter values are set here. There should be no values set in the
    // variable definitions.
    private void GetInitialParameterValues(IConfigSource config)
    {
        ConfigurationParameters parms = new ConfigurationParameters();
        UnmanagedParams[0] = parms;

        BSParam.SetParameterDefaultValues(this);

        if (config != null)
        {
            // If there are specifications in the ini file, use those values
            IConfig pConfig = config.Configs["BulletSim"];
            if (pConfig != null)
            {
                BSParam.SetParameterConfigurationValues(this, pConfig);

                // There are two Bullet implementations to choose from
                BulletEngineName = pConfig.GetString("BulletEngine", "BulletUnmanaged");

                // Very detailed logging for physics debugging
                // TODO: the boolean values can be moved to the normal parameter processing.
                m_physicsLoggingEnabled = pConfig.GetBoolean("PhysicsLoggingEnabled", false);
                m_physicsLoggingDir = pConfig.GetString("PhysicsLoggingDir", ".");
                m_physicsLoggingPrefix = pConfig.GetString("PhysicsLoggingPrefix", "physics-%REGIONNAME%-");
                m_physicsLoggingFileMinutes = pConfig.GetInt("PhysicsLoggingFileMinutes", 5);
                m_physicsLoggingDoFlush = pConfig.GetBoolean("PhysicsLoggingDoFlush", false);
                m_physicsPhysicalDumpEnabled = pConfig.GetBoolean("PhysicsPhysicalDumpEnabled", false);
                // Very detailed logging for vehicle debugging
                VehicleLoggingEnabled = pConfig.GetBoolean("VehicleLoggingEnabled", false);
                VehiclePhysicalLoggingEnabled = pConfig.GetBoolean("VehiclePhysicalLoggingEnabled", false);

                // Do any replacements in the parameters
                m_physicsLoggingPrefix = m_physicsLoggingPrefix.Replace("%REGIONNAME%", RegionName);
            }
            else
            {
                // Nothing in the configuration INI file so assume unmanaged and other defaults.
                BulletEngineName = "BulletUnmanaged";
                m_physicsLoggingEnabled = false;
                VehicleLoggingEnabled = false;
            }

            // The material characteristics.
            BSMaterials.InitializeFromDefaults(Params);
            if (pConfig != null)
            {
                // Let the user add new and interesting material property values.
                BSMaterials.InitializefromParameters(pConfig);
            }
        }
    }

    // A helper function that handles a true/false parameter and returns the proper float number encoding
    float ParamBoolean(IConfig config, string parmName, float deflt)
    {
        float ret = deflt;
        if (config.Contains(parmName))
        {
            ret = ConfigurationParameters.numericFalse;
            if (config.GetBoolean(parmName, false))
            {
                ret = ConfigurationParameters.numericTrue;
            }
        }
        return ret;
    }

    // Select the connection to the actual Bullet implementation.
    // The main engine selection is the engineName up to the first hypen.
    // So "Bullet-2.80-OpenCL-Intel" specifies the 'bullet' class here and the whole name
    //     is passed to the engine to do its special selection, etc.
    private BSAPITemplate SelectUnderlyingBulletEngine(string engineName)
    {
        // For the moment, do a simple switch statement.
        // Someday do fancyness with looking up the interfaces in the assembly.
        BSAPITemplate ret = null;

        string selectionName = engineName.ToLower();
        int hyphenIndex = engineName.IndexOf("-");
        if (hyphenIndex > 0)
            selectionName = engineName.ToLower().Substring(0, hyphenIndex - 1);

        switch (selectionName)
        {
            case "bullet":
            case "bulletunmanaged":
                ret = new BSAPIUnman(engineName, this);
                break;
            case "bulletxna":
                ret = new BSAPIXNA(engineName, this);
                // Disable some features that are not implemented in BulletXNA
                m_log.InfoFormat("{0} Disabling some physics features not implemented by BulletXNA", LogHeader);
                m_log.InfoFormat("{0}    Disabling ShouldUseBulletHACD", LogHeader);
                BSParam.ShouldUseBulletHACD = false;
                m_log.InfoFormat("{0}    Disabling ShouldUseSingleConvexHullForPrims", LogHeader);
                BSParam.ShouldUseSingleConvexHullForPrims = false;
                m_log.InfoFormat("{0}    Disabling ShouldUseGImpactShapeForPrims", LogHeader);
                BSParam.ShouldUseGImpactShapeForPrims = false;
                m_log.InfoFormat("{0}    Setting terrain implimentation to Heightmap", LogHeader);
                BSParam.TerrainImplementation = (float)BSTerrainPhys.TerrainImplementation.Heightmap;
                break;
        }

        if (ret == null)
        {
            m_log.ErrorFormat("{0) COULD NOT SELECT BULLET ENGINE: '[BulletSim]PhysicsEngine' must be either 'BulletUnmanaged-*' or 'BulletXNA-*'", LogHeader);
        }
        else
        {
            m_log.InfoFormat("{0} Selected bullet engine {1} -> {2}/{3}", LogHeader, engineName, ret.BulletEngineName, ret.BulletEngineVersion);
        }

        return ret;
    }

    public override void Dispose()
    {
        // m_log.DebugFormat("{0}: Dispose()", LogHeader);

        // make sure no stepping happens while we're deleting stuff
        m_initialized = false;

        foreach (KeyValuePair<uint, BSPhysObject> kvp in PhysObjects)
        {
            kvp.Value.Destroy();
        }
        PhysObjects.Clear();

        // Now that the prims are all cleaned up, there should be no constraints left
        if (Constraints != null)
        {
            Constraints.Dispose();
            Constraints = null;
        }

        if (Shapes != null)
        {
            Shapes.Dispose();
            Shapes = null;
        }

        if (TerrainManager != null)
        {
            TerrainManager.ReleaseGroundPlaneAndTerrain();
            TerrainManager.Dispose();
            TerrainManager = null;
        }

        // Anything left in the unmanaged code should be cleaned out
        PE.Shutdown(World);

        // Not logging any more
        PhysicsLogging.Close();
    }
    #endregion // Construction and Initialization

    #region Prim and Avatar addition and removal

    public override PhysicsActor AddAvatar(string avName, Vector3 position, Vector3 size, bool isFlying)
    {
        m_log.ErrorFormat("{0}: CALL TO AddAvatar in BSScene. NOT IMPLEMENTED", LogHeader);
        return null;
    }

    public override PhysicsActor AddAvatar(uint localID, string avName, Vector3 position, Vector3 size, bool isFlying)
    {
        // m_log.DebugFormat("{0}: AddAvatar: {1}", LogHeader, avName);

        if (!m_initialized) return null;

        BSCharacter actor = new BSCharacter(localID, avName, this, position, size, isFlying);
        lock (PhysObjects)
            PhysObjects.Add(localID, actor);

        // TODO: Remove kludge someday.
        // We must generate a collision for avatars whether they collide or not.
        // This is required by OpenSim to update avatar animations, etc.
        lock (m_avatars)
            m_avatars.Add(actor);

        return actor;
    }

    public override void RemoveAvatar(PhysicsActor actor)
    {
        // m_log.DebugFormat("{0}: RemoveAvatar", LogHeader);

        if (!m_initialized) return;

        BSCharacter bsactor = actor as BSCharacter;
        if (bsactor != null)
        {
            try
            {
                lock (PhysObjects)
                    PhysObjects.Remove(bsactor.LocalID);
                // Remove kludge someday
                lock (m_avatars)
                    m_avatars.Remove(bsactor);
            }
            catch (Exception e)
            {
                m_log.WarnFormat("{0}: Attempt to remove avatar that is not in physics scene: {1}", LogHeader, e);
            }
            bsactor.Destroy();
            // bsactor.dispose();
        }
        else
        {
            m_log.ErrorFormat("{0}: Requested to remove avatar that is not a BSCharacter. ID={1}, type={2}",
                                        LogHeader, actor.LocalID, actor.GetType().Name);
        }
    }

    public override void RemovePrim(PhysicsActor prim)
    {
        if (!m_initialized) return;

        BSPhysObject bsprim = prim as BSPhysObject;
        if (bsprim != null)
        {
            DetailLog("{0},RemovePrim,call", bsprim.LocalID);
            // m_log.DebugFormat("{0}: RemovePrim. id={1}/{2}", LogHeader, bsprim.Name, bsprim.LocalID);
            try
            {
                lock (PhysObjects) PhysObjects.Remove(bsprim.LocalID);
            }
            catch (Exception e)
            {
                m_log.ErrorFormat("{0}: Attempt to remove prim that is not in physics scene: {1}", LogHeader, e);
            }
            bsprim.Destroy();
            // bsprim.dispose();
        }
        else
        {
            m_log.ErrorFormat("{0}: Attempt to remove prim that is not a BSPrim type.", LogHeader);
        }
    }

    public override PhysicsActor AddPrimShape(string primName, PrimitiveBaseShape pbs, Vector3 position,
                                              Vector3 size, Quaternion rotation, bool isPhysical, uint localID)
    {
        // m_log.DebugFormat("{0}: AddPrimShape2: {1}", LogHeader, primName);

        if (!m_initialized) return null;

        // DetailLog("{0},BSScene.AddPrimShape,call", localID);

        BSPhysObject prim = new BSPrimLinkable(localID, primName, this, position, size, rotation, pbs, isPhysical);
        lock (PhysObjects) PhysObjects.Add(localID, prim);
        return prim;
    }

    // This is a call from the simulator saying that some physical property has been updated.
    // The BulletSim driver senses the changing of relevant properties so this taint
    // information call is not needed.
    public override void AddPhysicsActorTaint(PhysicsActor prim) { }

    #endregion // Prim and Avatar addition and removal

    #region Simulation

    // Call from the simulator to send physics information to the simulator objects.
    // This pushes all the collision and property update events into the objects in
    //    the simulator and, since it is on the heartbeat thread, there is an implicit
    //    locking of those data structures from other heartbeat events.
    // If the physics engine is running on a separate thread, the update information
    //    will be in the ObjectsWithCollions and ObjectsWithUpdates structures.
    public override float Simulate(float timeStep)
    {
        if (!BSParam.UseSeparatePhysicsThread)
        {
            DoPhysicsStep(timeStep);
        }
        return SendUpdatesToSimulator(timeStep);
    }

    // Call the physics engine to do one 'timeStep' and collect collisions and updates
    //    into ObjectsWithCollisions and ObjectsWithUpdates data structures.
    private void DoPhysicsStep(float timeStep)
    {
        // prevent simulation until we've been initialized
        if (!m_initialized) return;

        LastTimeStep = timeStep;

        int updatedEntityCount = 0;
        int collidersCount = 0;

        int beforeTime = Util.EnvironmentTickCount();
        int simTime = 0;

        int numTaints = _taintOperations.Count;
        InTaintTime = true; // Only used for debugging so locking is not necessary.

        // update the prim states while we know the physics engine is not busy
        ProcessTaints();

        // Some of the physical objects requre individual, pre-step calls
        //      (vehicles and avatar movement, in particular)
        TriggerPreStepEvent(timeStep);

        // the prestep actions might have added taints
        numTaints += _taintOperations.Count;
        ProcessTaints();

        InTaintTime = false; // Only used for debugging so locking is not necessary.

        // The following causes the unmanaged code to output ALL the values found in ALL the objects in the world.
        // Only enable this in a limited test world with few objects.
        if (m_physicsPhysicalDumpEnabled)
            PE.DumpAllInfo(World);

        // step the physical world one interval
        m_simulationStep++;
        int numSubSteps = 0;
        try
        {
            numSubSteps = PE.PhysicsStep(World, timeStep, m_maxSubSteps, m_fixedTimeStep, out updatedEntityCount, out collidersCount);

        }
        catch (Exception e)
        {
            m_log.WarnFormat("{0},PhysicsStep Exception: nTaints={1}, substeps={2}, updates={3}, colliders={4}, e={5}",
                        LogHeader, numTaints, numSubSteps, updatedEntityCount, collidersCount, e);
            DetailLog("{0},PhysicsStepException,call, nTaints={1}, substeps={2}, updates={3}, colliders={4}",
                        DetailLogZero, numTaints, numSubSteps, updatedEntityCount, collidersCount);
            updatedEntityCount = 0;
            collidersCount = 0;
        }

        // Make the physics engine dump useful statistics periodically
        if (PhysicsMetricDumpFrames != 0 && ((m_simulationStep % PhysicsMetricDumpFrames) == 0))
            PE.DumpPhysicsStatistics(World);

        // Get a value for 'now' so all the collision and update routines don't have to get their own.
        SimulationNowTime = Util.EnvironmentTickCount();

        // Send collision information to the colliding objects. The objects decide if the collision
        //     is 'real' (like linksets don't collide with themselves) and the individual objects
        //     know if the simulator has subscribed to collisions.
        lock (CollisionLock)
        {
            if (collidersCount > 0)
            {
                for (int ii = 0; ii < collidersCount; ii++)
                {
                    uint cA = m_collisionArray[ii].aID;
                    uint cB = m_collisionArray[ii].bID;
                    Vector3 point = m_collisionArray[ii].point;
                    Vector3 normal = m_collisionArray[ii].normal;
                    float penetration = m_collisionArray[ii].penetration;
                    SendCollision(cA, cB, point, normal, penetration);
                    SendCollision(cB, cA, point, -normal, penetration);
                }
            }
        }

        // If any of the objects had updated properties, tell the managed objects about the update
        //     and remember that there was a change so it will be passed to the simulator.
        lock (UpdateLock)
        {
            if (updatedEntityCount > 0)
            {
                for (int ii = 0; ii < updatedEntityCount; ii++)
                {
                    EntityProperties entprop = m_updateArray[ii];
                    BSPhysObject pobj;
                    if (PhysObjects.TryGetValue(entprop.ID, out pobj))
                    {
                        pobj.UpdateProperties(entprop);
                    }
                }
            }
        }

        // Some actors want to know when the simulation step is complete.
        TriggerPostStepEvent(timeStep);

        simTime = Util.EnvironmentTickCountSubtract(beforeTime);
        if (PhysicsLogging.Enabled)
        {
            DetailLog("{0},DoPhysicsStep,complete,frame={1}, nTaints={2}, simTime={3}, substeps={4}, updates={5}, colliders={6}, objWColl={7}",
                                    DetailLogZero, m_simulationStep, numTaints, simTime, numSubSteps,
                                    updatedEntityCount, collidersCount, ObjectsWithCollisions.Count);
        }

        // The following causes the unmanaged code to output ALL the values found in ALL the objects in the world.
        // Only enable this in a limited test world with few objects.
        if (m_physicsPhysicalDumpEnabled)
            PE.DumpAllInfo(World);

        // The physics engine returns the number of milliseconds it simulated this call.
        // These are summed and normalized to one second and divided by 1000 to give the reported physics FPS.
        // Multiply by a fixed nominal frame rate to give a rate similar to the simulator (usually 55).
        m_simulatedTime +=  (float)numSubSteps * m_fixedTimeStep * 1000f * NominalFrameRate;
    }

    // Called by a BSPhysObject to note that it has changed properties and this information
    //    should be passed up to the simulator at the proper time.
    // Note: this is called by the BSPhysObject from invocation via DoPhysicsStep() above so
    //    this is is under UpdateLock.
    public void PostUpdate(BSPhysObject updatee)
    {
        ObjectsWithUpdates.Add(updatee);
    }

    // The simulator thinks it is physics time so return all the collisions and position
    //     updates that were collected in actual physics simulation.
    private float SendUpdatesToSimulator(float timeStep)
    {
        if (!m_initialized) return 5.0f;

        DetailLog("{0},SendUpdatesToSimulator,collisions={1},updates={2},simedTime={3}",
            BSScene.DetailLogZero, ObjectsWithCollisions.Count, ObjectsWithUpdates.Count, m_simulatedTime);
        // Push the collisions into the simulator.
        lock (CollisionLock)
        {
            if (ObjectsWithCollisions.Count > 0)
            {
                foreach (BSPhysObject bsp in ObjectsWithCollisions)
                    if (!bsp.SendCollisions())
                    {
                        // If the object is done colliding, see that it's removed from the colliding list
                        ObjectsWithNoMoreCollisions.Add(bsp);
                    }
            }

            // This is a kludge to get avatar movement updates.
            // The simulator expects collisions for avatars even if there are have been no collisions.
            //    The event updates avatar animations and stuff.
            // If you fix avatar animation updates, remove this overhead and let normal collision processing happen.
            foreach (BSPhysObject bsp in m_avatars)
                if (!ObjectsWithCollisions.Contains(bsp))   // don't call avatars twice
                    bsp.SendCollisions();

            // Objects that are done colliding are removed from the ObjectsWithCollisions list.
            // Not done above because it is inside an iteration of ObjectWithCollisions.
            // This complex collision processing is required to create an empty collision
            //     event call after all real collisions have happened on an object. This allows
            //     the simulator to generate the 'collision end' event.
            if (ObjectsWithNoMoreCollisions.Count > 0)
            {
                foreach (BSPhysObject po in ObjectsWithNoMoreCollisions)
                    ObjectsWithCollisions.Remove(po);
                ObjectsWithNoMoreCollisions.Clear();
            }
        }

        // Call the simulator for each object that has physics property updates.
        HashSet<BSPhysObject> updatedObjects = null;
        lock (UpdateLock)
        {
            if (ObjectsWithUpdates.Count > 0)
            {
                updatedObjects = ObjectsWithUpdates;
                ObjectsWithUpdates = new HashSet<BSPhysObject>();
            }
        }
        if (updatedObjects != null)
        {
            foreach (BSPhysObject obj in updatedObjects)
            {
                obj.RequestPhysicsterseUpdate();
            }
            updatedObjects.Clear();
        }

        // Return the framerate simulated to give the above returned results.
        // (Race condition here but this is just bookkeeping so rare mistakes do not merit a lock).
        float simTime = m_simulatedTime;
        m_simulatedTime = 0f;
        return simTime;
    }

    // Something has collided
    private void SendCollision(uint localID, uint collidingWith, Vector3 collidePoint, Vector3 collideNormal, float penetration)
    {
        if (localID <= TerrainManager.HighestTerrainID)
        {
            return;         // don't send collisions to the terrain
        }

        BSPhysObject collider;
        if (!PhysObjects.TryGetValue(localID, out collider))
        {
            // If the object that is colliding cannot be found, just ignore the collision.
            DetailLog("{0},BSScene.SendCollision,colliderNotInObjectList,id={1},with={2}", DetailLogZero, localID, collidingWith);
            return;
        }

        // Note: the terrain is not in the physical object list so 'collidee' can be null when Collide() is called.
        BSPhysObject collidee = null;
        PhysObjects.TryGetValue(collidingWith, out collidee);

        // DetailLog("{0},BSScene.SendCollision,collide,id={1},with={2}", DetailLogZero, localID, collidingWith);

        if (collider.Collide(collidingWith, collidee, collidePoint, collideNormal, penetration))
        {
            // If a collision was 'good', remember to send it to the simulator
            ObjectsWithCollisions.Add(collider);
        }

        return;
    }

    public void BulletSPluginPhysicsThread()
    {
        while (m_initialized)
        {
            int beginSimulationRealtimeMS = Util.EnvironmentTickCount();
            DoPhysicsStep(BSParam.PhysicsTimeStep);
            int simulationRealtimeMS = Util.EnvironmentTickCountSubtract(beginSimulationRealtimeMS);
            int simulationTimeVsRealtimeDifferenceMS = ((int)(BSParam.PhysicsTimeStep*1000f)) - simulationRealtimeMS;

            if (simulationTimeVsRealtimeDifferenceMS > 0)
            {
                // The simulation of the time interval took less than realtime.
                // Do a sleep for the rest of realtime.
                Thread.Sleep(simulationTimeVsRealtimeDifferenceMS);
            }
            else
            {
                // The simulation took longer than realtime.
                // Do some scaling of simulation time.
                // TODO.
                DetailLog("{0},BulletSPluginPhysicsThread,longerThanRealtime={1}", BSScene.DetailLogZero, simulationTimeVsRealtimeDifferenceMS);
            }
        }
    }

    #endregion // Simulation

    public override void GetResults() { }

    #region Terrain

    public override void SetTerrain(float[] heightMap) {
        TerrainManager.SetTerrain(heightMap);
    }

    public override void SetWaterLevel(float baseheight)
    {
        SimpleWaterLevel = baseheight;
    }

    public override void DeleteTerrain()
    {
        // m_log.DebugFormat("{0}: DeleteTerrain()", LogHeader);
    }

    // Although no one seems to check this, I do support combining.
    public override bool SupportsCombining()
    {
        return TerrainManager.SupportsCombining();
    }
    // This call says I am a child to region zero in a mega-region. 'pScene' is that
    //    of region zero, 'offset' is my offset from regions zero's origin, and
    //    'extents' is the largest XY that is handled in my region.
    public override void Combine(PhysicsScene pScene, Vector3 offset, Vector3 extents)
    {
        TerrainManager.Combine(pScene, offset, extents);
    }

    // Unhook all the combining that I know about.
    public override void UnCombine(PhysicsScene pScene)
    {
        TerrainManager.UnCombine(pScene);
    }

    #endregion // Terrain

    public override Dictionary<uint, float> GetTopColliders()
    {
        Dictionary<uint, float> topColliders;

        lock (PhysObjects)
        {
            foreach (KeyValuePair<uint, BSPhysObject> kvp in PhysObjects)
            {
                kvp.Value.ComputeCollisionScore();
            }

            List<BSPhysObject> orderedPrims = new List<BSPhysObject>(PhysObjects.Values);
            orderedPrims.OrderByDescending(p => p.CollisionScore);
            topColliders = orderedPrims.Take(25).ToDictionary(p => p.LocalID, p => p.CollisionScore);
        }

        return topColliders;
    }

    public override bool IsThreaded { get { return false;  } }

    #region Extensions
    // =============================================================
    // Per scene functions. See below.

    // Per avatar functions. See BSCharacter.

    // Per prim functions. See BSPrim.
    public const string PhysFunctGetLinksetType = "BulletSim.GetLinksetType";
    public const string PhysFunctSetLinksetType = "BulletSim.SetLinksetType";
    // =============================================================

    public override object Extension(string pFunct, params object[] pParams)
    {
        return base.Extension(pFunct, pParams);
    }
    #endregion // Extensions

    #region Taints
    // The simulation execution order is:
    // Simulate()
    //    DoOneTimeTaints
    //    TriggerPreStepEvent
    //    DoOneTimeTaints
    //    Step()
    //       ProcessAndSendToSimulatorCollisions
    //       ProcessAndSendToSimulatorPropertyUpdates
    //    TriggerPostStepEvent

    // Calls to the PhysicsActors can't directly call into the physics engine
    //       because it might be busy. We delay changes to a known time.
    // We rely on C#'s closure to save and restore the context for the delegate.
    public void TaintedObject(String ident, TaintCallback callback)
    {
        if (!m_initialized) return;

        lock (_taintLock)
        {
            _taintOperations.Add(new TaintCallbackEntry(ident, callback));
        }

        return;
    }

    // Sometimes a potentially tainted operation can be used in and out of taint time.
    // This routine executes the command immediately if in taint-time otherwise it is queued.
    public void TaintedObject(bool inTaintTime, string ident, TaintCallback callback)
    {
        if (inTaintTime)
            callback();
        else
            TaintedObject(ident, callback);
    }

    private void TriggerPreStepEvent(float timeStep)
    {
        PreStepAction actions = BeforeStep;
        if (actions != null)
            actions(timeStep);

    }

    private void TriggerPostStepEvent(float timeStep)
    {
        PostStepAction actions = AfterStep;
        if (actions != null)
            actions(timeStep);

    }

    // When someone tries to change a property on a BSPrim or BSCharacter, the object queues
    // a callback into itself to do the actual property change. That callback is called
    // here just before the physics engine is called to step the simulation.
    public void ProcessTaints()
    {
        ProcessRegularTaints();
        ProcessPostTaintTaints();
    }

    private void ProcessRegularTaints()
    {
        if (_taintOperations.Count > 0)  // save allocating new list if there is nothing to process
        {
            // swizzle a new list into the list location so we can process what's there
            List<TaintCallbackEntry> oldList;
            lock (_taintLock)
            {
                oldList = _taintOperations;
                _taintOperations = new List<TaintCallbackEntry>();
            }

            foreach (TaintCallbackEntry tcbe in oldList)
            {
                try
                {
                    DetailLog("{0},BSScene.ProcessTaints,doTaint,id={1}", DetailLogZero, tcbe.ident); // DEBUG DEBUG DEBUG
                    tcbe.callback();
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat("{0}: ProcessTaints: {1}: Exception: {2}", LogHeader, tcbe.ident, e);
                }
            }
            oldList.Clear();
        }
    }

    // Schedule an update to happen after all the regular taints are processed.
    // Note that new requests for the same operation ("ident") for the same object ("ID")
    //     will replace any previous operation by the same object.
    public void PostTaintObject(String ident, uint ID, TaintCallback callback)
    {
        string uniqueIdent = ident + "-" + ID.ToString();
        lock (_taintLock)
        {
            _postTaintOperations[uniqueIdent] = new TaintCallbackEntry(uniqueIdent, callback);
        }

        return;
    }

    // Taints that happen after the normal taint processing but before the simulation step.
    private void ProcessPostTaintTaints()
    {
        if (_postTaintOperations.Count > 0)
        {
            Dictionary<string, TaintCallbackEntry> oldList;
            lock (_taintLock)
            {
                oldList = _postTaintOperations;
                _postTaintOperations = new Dictionary<string, TaintCallbackEntry>();
            }

            foreach (KeyValuePair<string,TaintCallbackEntry> kvp in oldList)
            {
                try
                {
                    DetailLog("{0},BSScene.ProcessPostTaintTaints,doTaint,id={1}", DetailLogZero, kvp.Key); // DEBUG DEBUG DEBUG
                    kvp.Value.callback();
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat("{0}: ProcessPostTaintTaints: {1}: Exception: {2}", LogHeader, kvp.Key, e);
                }
            }
            oldList.Clear();
        }
    }

    // Only used for debugging. Does not change state of anything so locking is not necessary.
    public bool AssertInTaintTime(string whereFrom)
    {
        if (!InTaintTime)
        {
            DetailLog("{0},BSScene.AssertInTaintTime,NOT IN TAINT TIME,Region={1},Where={2}", DetailLogZero, RegionName, whereFrom);
            m_log.ErrorFormat("{0} NOT IN TAINT TIME!! Region={1}, Where={2}", LogHeader, RegionName, whereFrom);
            // Util.PrintCallStack(DetailLog);
        }
        return InTaintTime;
    }

    #endregion // Taints

    #region IPhysicsParameters
    // Get the list of parameters this physics engine supports
    public PhysParameterEntry[] GetParameterList()
    {
        BSParam.BuildParameterTable();
        return BSParam.SettableParameters;
    }

    // Set parameter on a specific or all instances.
    // Return 'false' if not able to set the parameter.
    // Setting the value in the m_params block will change the value the physics engine
    //   will use the next time since it's pinned and shared memory.
    // Some of the values require calling into the physics engine to get the new
    //   value activated ('terrainFriction' for instance).
    public bool SetPhysicsParameter(string parm, string val, uint localID)
    {
        bool ret = false;

        BSParam.ParameterDefnBase theParam;
        if (BSParam.TryGetParameter(parm, out theParam))
        {
            // Set the value in the C# code
            theParam.SetValue(this, val);

            // Optionally set the parameter in the unmanaged code
            if (theParam.HasSetOnObject)
            {
                // update all the localIDs specified
                // If the local ID is APPLY_TO_NONE, just change the default value
                // If the localID is APPLY_TO_ALL change the default value and apply the new value to all the lIDs
                // If the localID is a specific object, apply the parameter change to only that object
                List<uint> objectIDs = new List<uint>();
                switch (localID)
                {
                    case PhysParameterEntry.APPLY_TO_NONE:
                        // This will cause a call into the physical world if some operation is specified (SetOnObject).
                        objectIDs.Add(TERRAIN_ID);
                        TaintedUpdateParameter(parm, objectIDs, val);
                        break;
                    case PhysParameterEntry.APPLY_TO_ALL:
                        lock (PhysObjects) objectIDs = new List<uint>(PhysObjects.Keys);
                        TaintedUpdateParameter(parm, objectIDs, val);
                        break;
                    default:
                        // setting only one localID
                        objectIDs.Add(localID);
                        TaintedUpdateParameter(parm, objectIDs, val);
                        break;
                }
            }

            ret = true;
        }
        return ret;
    }

    // schedule the actual updating of the paramter to when the phys engine is not busy
    private void TaintedUpdateParameter(string parm, List<uint> lIDs, string val)
    {
        string xval = val;
        List<uint> xlIDs = lIDs;
        string xparm = parm;
        TaintedObject("BSScene.UpdateParameterSet", delegate() {
            BSParam.ParameterDefnBase thisParam;
            if (BSParam.TryGetParameter(xparm, out thisParam))
            {
                if (thisParam.HasSetOnObject)
                {
                    foreach (uint lID in xlIDs)
                    {
                        BSPhysObject theObject = null;
                        if (PhysObjects.TryGetValue(lID, out theObject))
                            thisParam.SetOnObject(this, theObject);
                    }
                }
            }
        });
    }

    // Get parameter.
    // Return 'false' if not able to get the parameter.
    public bool GetPhysicsParameter(string parm, out string value)
    {
        string val = String.Empty;
        bool ret = false;
        BSParam.ParameterDefnBase theParam;
        if (BSParam.TryGetParameter(parm, out theParam))
        {
            val = theParam.GetValue(this);
            ret = true;
        }
        value = val;
        return ret;
    }

    #endregion IPhysicsParameters

    // Invoke the detailed logger and output something if it's enabled.
    public void DetailLog(string msg, params Object[] args)
    {
        PhysicsLogging.Write(msg, args);
    }
    // Used to fill in the LocalID when there isn't one. It's the correct number of characters.
    public const string DetailLogZero = "0000000000";

}
}