Inheritance: IParcelManagementModule
        /*
         * 20140215 radams1: The border stuff was removed and the addition of regions to the mega-regions
         *   was generalized. These functions are not needed for the generalized solution but left for reference.
         * private bool DoWorkForOneRegionOverXPlusY(RegionConnections rootConn, RegionConnections newConn, Scene scene)
         * {
         *  Vector3 offset = Vector3.Zero;
         *  offset.X = newConn.PosX - rootConn.PosX;
         *  offset.Y = newConn.PosY - rootConn.PosY;
         *
         *  Vector3 extents = Vector3.Zero;
         *  extents.Y = newConn.YEnd + rootConn.YEnd;
         *  extents.X = rootConn.XEnd;
         *  rootConn.UpdateExtents(extents);
         *
         *  RegionData ConnectedRegion = new RegionData();
         *  ConnectedRegion.Offset = offset;
         *  ConnectedRegion.RegionId = scene.RegionInfo.originRegionID;
         *  ConnectedRegion.RegionScene = scene;
         *  rootConn.ConnectedRegions.Add(ConnectedRegion);
         *
         *  m_log.DebugFormat(
         *      "[REGION COMBINER MODULE]: Root region {0} is to the south of region {1}, Offset: {2}, Extents: {3}",
         *      rootConn.RegionScene.RegionInfo.RegionName,
         *      newConn.RegionScene.RegionInfo.RegionName, offset, extents);
         *
         *  rootConn.RegionScene.PhysicsScene.Combine(null, Vector3.Zero, extents);
         *  scene.PhysicsScene.Combine(rootConn.RegionScene.PhysicsScene, offset, Vector3.Zero);
         *
         *  // Reset Terrain..  since terrain normally loads first.
         *  //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
         *  scene.PhysicsScene.SetTerrain(scene.Heightmap.GetFloatsSerialised());
         *  //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
         *
         *  if (rootConn.ClientEventForwarder != null)
         *      rootConn.ClientEventForwarder.AddSceneToEventForwarding(scene);
         *
         *  return true;
         * }
         *
         * private bool DoWorkForOneRegionOverPlusXPlusY(RegionConnections rootConn, RegionConnections newConn, Scene scene)
         * {
         *  Vector3 offset = Vector3.Zero;
         *  offset.X = newConn.PosX - rootConn.PosX;
         *  offset.Y = newConn.PosY - rootConn.PosY;
         *
         *  Vector3 extents = Vector3.Zero;
         *
         *  // We do not want to inflate the extents for regions strictly to the NE of the root region, since this
         *  // would double count regions strictly to the north and east that have already been added.
         * //            extents.Y = regionConnections.YEnd + conn.YEnd;
         * //            extents.X = regionConnections.XEnd + conn.XEnd;
         * //            conn.UpdateExtents(extents);
         *
         *  extents.Y = rootConn.YEnd;
         *  extents.X = rootConn.XEnd;
         *
         *  RegionData ConnectedRegion = new RegionData();
         *  ConnectedRegion.Offset = offset;
         *  ConnectedRegion.RegionId = scene.RegionInfo.originRegionID;
         *  ConnectedRegion.RegionScene = scene;
         *
         *  rootConn.ConnectedRegions.Add(ConnectedRegion);
         *
         *  m_log.DebugFormat(
         *      "[REGION COMBINER MODULE]: Region {0} is to the southwest of Scene {1}, Offset: {2}, Extents: {3}",
         *      rootConn.RegionScene.RegionInfo.RegionName,
         *      newConn.RegionScene.RegionInfo.RegionName, offset, extents);
         *
         *  rootConn.RegionScene.PhysicsScene.Combine(null, Vector3.Zero, extents);
         *  scene.PhysicsScene.Combine(rootConn.RegionScene.PhysicsScene, offset, Vector3.Zero);
         *
         *  // Reset Terrain..  since terrain normally loads first.
         *  //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
         *  scene.PhysicsScene.SetTerrain(scene.Heightmap.GetFloatsSerialised());
         *  //conn.RegionScene.PhysicsScene.SetTerrain(conn.RegionScene.Heightmap.GetFloatsSerialised());
         *
         *  if (rootConn.ClientEventForwarder != null)
         *      rootConn.ClientEventForwarder.AddSceneToEventForwarding(scene);
         *
         *  return true;
         *
         *  //scene.PhysicsScene.Combine(conn.RegionScene.PhysicsScene, offset,extents);
         * }
         */

        private void DoWorkForRootRegion(RegionConnections rootConn, Scene scene)
        {
            m_log.DebugFormat("[REGION COMBINER MODULE]: Adding root region {0}", scene.RegionInfo.RegionName);

            RegionData rdata = new RegionData();

            rdata.Offset      = Vector3.Zero;
            rdata.RegionId    = scene.RegionInfo.originRegionID;
            rdata.RegionScene = scene;
            // save it's land channel
            rootConn.RegionLandChannel = scene.LandChannel;

            // Substitue our landchannel
            RegionCombinerLargeLandChannel lnd = new RegionCombinerLargeLandChannel(rdata, scene.LandChannel,
                                                                                    rootConn.ConnectedRegions);

            scene.LandChannel = lnd;

            // Forward the permissions modules of each of the connected regions to the root region
            lock (m_regions)
            {
                foreach (RegionData r in rootConn.ConnectedRegions)
                {
                    ForwardPermissionRequests(rootConn, r.RegionScene);
                }

                // Create the root region's Client Event Forwarder
                rootConn.ClientEventForwarder = new RegionCombinerClientEventForwarder(rootConn);

                // Sets up the CoarseLocationUpdate forwarder for this root region
                scene.EventManager.OnNewPresence += SetCoarseLocationDelegate;

                // Adds this root region to a dictionary of regions that are connectable
                m_regions.Add(scene.RegionInfo.originRegionID, rootConn);
            }
        }
        private void DoWorkForRootRegion(RegionConnections regionConnections, Scene scene)
        {
            RegionData rdata = new RegionData();
            rdata.Offset = Vector3.Zero;
            rdata.RegionId = scene.RegionInfo.originRegionID;
            rdata.RegionScene = scene;
            // save it's land channel
            regionConnections.RegionLandChannel = scene.LandChannel;

            // Substitue our landchannel
            RegionCombinerLargeLandChannel lnd = new RegionCombinerLargeLandChannel(rdata, scene.LandChannel,
                                                            regionConnections.ConnectedRegions);
            scene.LandChannel = lnd;
            // Forward the permissions modules of each of the connected regions to the root region
            lock (m_regions)
            {
                foreach (RegionData r in regionConnections.ConnectedRegions)
                {
                    ForwardPermissionRequests(regionConnections, r.RegionScene);
                }
            }
            // Create the root region's Client Event Forwarder
            regionConnections.ClientEventForwarder = new RegionCombinerClientEventForwarder(regionConnections);

            // Sets up the CoarseLocationUpdate forwarder for this root region
            scene.EventManager.OnNewPresence += SetCourseLocationDelegate;

            // Adds this root region to a dictionary of regions that are connectable
            m_regions.Add(scene.RegionInfo.originRegionID, regionConnections);
        }
        private void DoWorkForRootRegion(RegionConnections regionConnections, Scene scene)
        {
            RegionData rdata = new RegionData();
            rdata.Offset = Vector3.Zero;
            rdata.RegionId = scene.RegionInfo.RegionID;
            rdata.RegionScene = scene;
            // save it's land channel
            regionConnections.RegionLandChannel = scene.RequestModuleInterface<IParcelManagementModule>();

            // Substitue our landchannel
            RegionCombinerLargeLandChannel lnd = new RegionCombinerLargeLandChannel(rdata, scene.RequestModuleInterface<IParcelManagementModule>(),
                                                            regionConnections.ConnectedRegions);
            scene.RegisterModuleInterface<IParcelManagementModule>(lnd);
            // Forward the permissions modules of each of the connected regions to the root region
            lock (m_regions)
            {
                foreach (RegionData r in regionConnections.ConnectedRegions)
                {
                    ForwardPermissionRequests(regionConnections, r.RegionScene);
                }
            }
            // Create the root region's Client Event Forwarder
            regionConnections.ClientEventForwarder = new RegionCombinerClientEventForwarder(regionConnections);

            // Sets up the CoarseLocationUpdate forwarder for this root region
            scene.EventManager.OnNewPresence += SetCourseLocationDelegate;

            scene.EventManager.OnNewClient += EventManager_OnNewClient;
            scene.EventManager.OnClosingClient += OnClosingClient;

            // Adds this root region to a dictionary of regions that are connectable
            m_regions.Add(scene.RegionInfo.RegionID, regionConnections);
            ITerrainChannel terrainHeightmap = scene.RequestModuleInterface<ITerrainChannel>();
            landTerrainChannel.AddRegion(rdata, terrainHeightmap);
        }
        private void RegionLoadedDoWork(Scene scene)
        {
            lock (m_startingScenes)
                m_startingScenes.Add(scene.RegionInfo.originRegionID, scene);

            // Give each region a standard set of non-infinite borders
            Border northBorder = new Border();
            northBorder.BorderLine = new Vector3(0, (int)Constants.RegionSize, (int)Constants.RegionSize);  //<---
            northBorder.CrossDirection = Cardinals.N;
            scene.NorthBorders[0] = northBorder;

            Border southBorder = new Border();
            southBorder.BorderLine = new Vector3(0, (int)Constants.RegionSize, 0);    //--->
            southBorder.CrossDirection = Cardinals.S;
            scene.SouthBorders[0] = southBorder;

            Border eastBorder = new Border();
            eastBorder.BorderLine = new Vector3(0, (int)Constants.RegionSize, (int)Constants.RegionSize);   //<---
            eastBorder.CrossDirection = Cardinals.E;
            scene.EastBorders[0] = eastBorder;

            Border westBorder = new Border();
            westBorder.BorderLine = new Vector3(0, (int)Constants.RegionSize, 0);     //--->
            westBorder.CrossDirection = Cardinals.W;
            scene.WestBorders[0] = westBorder;

            RegionConnections regionConnections = new RegionConnections();
            regionConnections.ConnectedRegions = new List<RegionData>();
            regionConnections.RegionScene = scene;
            regionConnections.RegionLandChannel = scene.LandChannel;
            regionConnections.RegionId = scene.RegionInfo.originRegionID;
            regionConnections.X = scene.RegionInfo.RegionLocX;
            regionConnections.Y = scene.RegionInfo.RegionLocY;
            regionConnections.XExtend = (int)Constants.RegionSize;
            regionConnections.YExtend = (int)Constants.RegionSize;


            if (m_nRegions == 0)
            {
                // this is root region
                regionConnections.XExtend *= m_numberCombinedRegionsX;
                regionConnections.YExtend *= m_numberCombinedRegionsY;

                m_rootScene = scene;
                m_rootConnection = regionConnections;

                // save it's land channel
                regionConnections.RegionLandChannel = scene.LandChannel;

                scene.RootScene = null;
                scene.RegionInfo.CombinedRegionHandle = 0;

                Vector3 extents;
                extents.X = regionConnections.XExtend;
                extents.Y = regionConnections.YExtend;
                extents.Z = 0;

                scene.RegionInfo.RegionSizeX = (uint)extents.X;
                scene.RegionInfo.RegionSizeY = (uint)extents.Y;

//                scene.PhysicsScene.Combine(null, Vector3.Zero, extents);

                lock (scene.SouthBorders)
                    scene.SouthBorders[0].BorderLine.Y = regionConnections.XExtend;

                lock (scene.WestBorders)
                    scene.WestBorders[0].BorderLine.Y = regionConnections.YExtend;

                lock (scene.EastBorders)
                {
                    scene.EastBorders[0].BorderLine.Z = regionConnections.XExtend;
                    scene.EastBorders[0].BorderLine.Y = regionConnections.YExtend;
                }

                lock (scene.NorthBorders)
                {
                    scene.NorthBorders[0].BorderLine.Z = regionConnections.YExtend;
                    scene.NorthBorders[0].BorderLine.Y = regionConnections.XExtend;
                }

                // Substitue our landchannel

                RegionData rdata = new RegionData();
                rdata.Offset = Vector3.Zero;
                rdata.RegionId = scene.RegionInfo.originRegionID;
                rdata.RegionScene = scene;

                RegionCombinerLargeLandChannel lnd = new RegionCombinerLargeLandChannel(rdata, scene.LandChannel,
                                                                regionConnections.ConnectedRegions);

                scene.LandChannel = lnd;
                // Forward the permissions modules of each of the connected regions to the root region
                // Create the root region's Client Event Forwarder
                regionConnections.ClientEventForwarder = new RegionCombinerClientEventForwarder(regionConnections);

                // Sets up the CoarseLocationUpdate forwarder for this root region
                scene.EventManager.OnNewPresence += SetCourseLocationDelegate;

                // Adds this root region to a dictionary of regions that are connectable
                m_regions.Add(scene.RegionInfo.originRegionID, regionConnections);
               
            }

            else if (m_rootScene != null)
            {
                scene.RootScene = m_rootScene;
                scene.RegionInfo.CombinedRegionHandle = m_rootScene.RegionInfo.RegionHandle;

                Vector3 offset = Vector3.Zero;
                offset.X = ((int)regionConnections.X - (int)m_rootConnection.X) * Constants.RegionSize;
                offset.Y = ((int)regionConnections.Y - (int)m_rootConnection.Y) * Constants.RegionSize;

                RegionData ConnectedRegion = new RegionData();
                ConnectedRegion.Offset = offset;
                ConnectedRegion.RegionId = scene.RegionInfo.originRegionID;
                ConnectedRegion.RegionScene = scene;
                m_rootConnection.ConnectedRegions.Add(ConnectedRegion);

                scene.Physics_Enabled = false;
                scene.Scripts_Enabled = false;


                m_rootScene.PhysicsScene.CombineTerrain(scene.Heightmap.GetFloatsSerialised(),offset);

                scene.EventManager.OnNewPresence += SetCourseLocationDelegate;
                // Create a client event forwarder and add this region's events to the root region.
                
                if (m_rootConnection.ClientEventForwarder != null)
                    m_rootConnection.ClientEventForwarder.AddSceneToEventForwarding(scene);
            }

            m_nRegions++;

            if (m_nRegions == m_nRegionsToLoad)
            {
                // final work
                lock (m_regions)
                {
                    foreach (RegionData r in regionConnections.ConnectedRegions)
                    {
                        ForwardPermissionRequests(regionConnections, r.RegionScene);
                    }
                }
            }

            AdjustLargeRegionBounds();
            return;

        }