clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyrightD
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 using System;
28 using System.Collections.Generic;
29 using System.Text;
30  
31 using OpenSim.Framework;
32 using OpenSim.Region.Framework;
33 using OpenSim.Region.CoreModules;
34 using OpenSim.Region.Physics.Manager;
35  
36 using Nini.Config;
37 using log4net;
38  
39 using OpenMetaverse;
40  
41 namespace OpenSim.Region.Physics.BulletSPlugin
42 {
43  
44 // The physical implementation of the terrain is wrapped in this class.
45 public abstract class BSTerrainPhys : IDisposable
46 {
47 public enum TerrainImplementation
48 {
49 Heightmap = 0,
50 Mesh = 1
51 }
52  
53 protected BSScene m_physicsScene { get; private set; }
54 // Base of the region in world coordinates. Coordinates inside the region are relative to this.
55 public Vector3 TerrainBase { get; private set; }
56 public uint ID { get; private set; }
57  
58 public BSTerrainPhys(BSScene physicsScene, Vector3 regionBase, uint id)
59 {
60 m_physicsScene = physicsScene;
61 TerrainBase = regionBase;
62 ID = id;
63 }
64 public abstract void Dispose();
65 public abstract float GetTerrainHeightAtXYZ(Vector3 pos);
66 public abstract float GetWaterLevelAtXYZ(Vector3 pos);
67 }
68  
69 // ==========================================================================================
70 public sealed class BSTerrainManager : IDisposable
71 {
72 static string LogHeader = "[BULLETSIM TERRAIN MANAGER]";
73  
74 // These height values are fractional so the odd values will be
75 // noticable when debugging.
76 public const float HEIGHT_INITIALIZATION = 24.987f;
77 public const float HEIGHT_INITIAL_LASTHEIGHT = 24.876f;
78 public const float HEIGHT_GETHEIGHT_RET = 24.765f;
79 public const float WATER_HEIGHT_GETHEIGHT_RET = 19.998f;
80  
81 // If the min and max height are equal, we reduce the min by this
82 // amount to make sure that a bounding box is built for the terrain.
83 public const float HEIGHT_EQUAL_FUDGE = 0.2f;
84  
85 // Until the whole simulator is changed to pass us the region size, we rely on constants.
86 public Vector3 DefaultRegionSize = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight);
87  
88 // The scene that I am part of
89 private BSScene m_physicsScene { get; set; }
90  
91 // The ground plane created to keep thing from falling to infinity.
92 private BulletBody m_groundPlane;
93  
94 // If doing mega-regions, if we're region zero we will be managing multiple
95 // region terrains since region zero does the physics for the whole mega-region.
96 private Dictionary<Vector3, BSTerrainPhys> m_terrains;
97  
98 // Flags used to know when to recalculate the height.
99 private bool m_terrainModified = false;
100  
101 // If we are doing mega-regions, terrains are added from TERRAIN_ID to m_terrainCount.
102 // This is incremented before assigning to new region so it is the last ID allocated.
103 private uint m_terrainCount = BSScene.CHILDTERRAIN_ID - 1;
104 public uint HighestTerrainID { get {return m_terrainCount; } }
105  
106 // If doing mega-regions, this holds our offset from region zero of
107 // the mega-regions. "parentScene" points to the PhysicsScene of region zero.
108 private Vector3 m_worldOffset;
109 // If the parent region (region 0), this is the extent of the combined regions
110 // relative to the origin of region zero
111 private Vector3 m_worldMax;
112 private PhysicsScene MegaRegionParentPhysicsScene { get; set; }
113  
114 public BSTerrainManager(BSScene physicsScene)
115 {
116 m_physicsScene = physicsScene;
117 m_terrains = new Dictionary<Vector3,BSTerrainPhys>();
118  
119 // Assume one region of default size
120 m_worldOffset = Vector3.Zero;
121 m_worldMax = new Vector3(DefaultRegionSize);
122 MegaRegionParentPhysicsScene = null;
123 }
124  
125 public void Dispose()
126 {
127 ReleaseGroundPlaneAndTerrain();
128 }
129  
130 // Create the initial instance of terrain and the underlying ground plane.
131 // This is called from the initialization routine so we presume it is
132 // safe to call Bullet in real time. We hope no one is moving prims around yet.
133 public void CreateInitialGroundPlaneAndTerrain()
134 {
135 DetailLog("{0},BSTerrainManager.CreateInitialGroundPlaneAndTerrain,region={1}", BSScene.DetailLogZero, m_physicsScene.RegionName);
136 // The ground plane is here to catch things that are trying to drop to negative infinity
137 BulletShape groundPlaneShape = m_physicsScene.PE.CreateGroundPlaneShape(BSScene.GROUNDPLANE_ID, 1f, BSParam.TerrainCollisionMargin);
138 m_groundPlane = m_physicsScene.PE.CreateBodyWithDefaultMotionState(groundPlaneShape,
139 BSScene.GROUNDPLANE_ID, Vector3.Zero, Quaternion.Identity);
140  
141 m_physicsScene.PE.AddObjectToWorld(m_physicsScene.World, m_groundPlane);
142 m_physicsScene.PE.UpdateSingleAabb(m_physicsScene.World, m_groundPlane);
143 // Ground plane does not move
144 m_physicsScene.PE.ForceActivationState(m_groundPlane, ActivationState.DISABLE_SIMULATION);
145 // Everything collides with the ground plane.
146 m_groundPlane.collisionType = CollisionType.Groundplane;
147 m_groundPlane.ApplyCollisionMask(m_physicsScene);
148  
149 BSTerrainPhys initialTerrain = new BSTerrainHeightmap(m_physicsScene, Vector3.Zero, BSScene.TERRAIN_ID, DefaultRegionSize);
150 lock (m_terrains)
151 {
152 // Build an initial terrain and put it in the world. This quickly gets replaced by the real region terrain.
153 m_terrains.Add(Vector3.Zero, initialTerrain);
154 }
155 }
156  
157 // Release all the terrain structures we might have allocated
158 public void ReleaseGroundPlaneAndTerrain()
159 {
160 DetailLog("{0},BSTerrainManager.ReleaseGroundPlaneAndTerrain,region={1}", BSScene.DetailLogZero, m_physicsScene.RegionName);
161 if (m_groundPlane.HasPhysicalBody)
162 {
163 if (m_physicsScene.PE.RemoveObjectFromWorld(m_physicsScene.World, m_groundPlane))
164 {
165 m_physicsScene.PE.DestroyObject(m_physicsScene.World, m_groundPlane);
166 }
167 m_groundPlane.Clear();
168 }
169  
170 ReleaseTerrain();
171 }
172  
173 // Release all the terrain we have allocated
174 public void ReleaseTerrain()
175 {
176 lock (m_terrains)
177 {
178 foreach (KeyValuePair<Vector3, BSTerrainPhys> kvp in m_terrains)
179 {
180 kvp.Value.Dispose();
181 }
182 m_terrains.Clear();
183 }
184 }
185  
186 // The simulator wants to set a new heightmap for the terrain.
187 public void SetTerrain(float[] heightMap) {
188 float[] localHeightMap = heightMap;
189 // If there are multiple requests for changes to the same terrain between ticks,
190 // only do that last one.
191 m_physicsScene.PostTaintObject("TerrainManager.SetTerrain-"+ m_worldOffset.ToString(), 0, delegate()
192 {
193 if (m_worldOffset != Vector3.Zero && MegaRegionParentPhysicsScene != null)
194 {
195 // If a child of a mega-region, we shouldn't have any terrain allocated for us
196 ReleaseGroundPlaneAndTerrain();
197 // If doing the mega-prim stuff and we are the child of the zero region,
198 // the terrain is added to our parent
199 if (MegaRegionParentPhysicsScene is BSScene)
200 {
201 DetailLog("{0},SetTerrain.ToParent,offset={1},worldMax={2}", BSScene.DetailLogZero, m_worldOffset, m_worldMax);
202 ((BSScene)MegaRegionParentPhysicsScene).TerrainManager.AddMegaRegionChildTerrain(
203 BSScene.CHILDTERRAIN_ID, localHeightMap, m_worldOffset, m_worldOffset + DefaultRegionSize);
204 }
205 }
206 else
207 {
208 // If not doing the mega-prim thing, just change the terrain
209 DetailLog("{0},SetTerrain.Existing", BSScene.DetailLogZero);
210  
211 UpdateTerrain(BSScene.TERRAIN_ID, localHeightMap, m_worldOffset, m_worldOffset + DefaultRegionSize);
212 }
213 });
214 }
215  
216 // Another region is calling this region and passing a terrain.
217 // A region that is not the mega-region root will pass its terrain to the root region so the root region
218 // physics engine will have all the terrains.
219 private void AddMegaRegionChildTerrain(uint id, float[] heightMap, Vector3 minCoords, Vector3 maxCoords)
220 {
221 // Since we are called by another region's thread, the action must be rescheduled onto our processing thread.
222 m_physicsScene.PostTaintObject("TerrainManager.AddMegaRegionChild" + minCoords.ToString(), id, delegate()
223 {
224 UpdateTerrain(id, heightMap, minCoords, maxCoords);
225 });
226 }
227  
228 // If called for terrain has has not been previously allocated, a new terrain will be built
229 // based on the passed information. The 'id' should be either the terrain id or
230 // BSScene.CHILDTERRAIN_ID. If the latter, a new child terrain ID will be allocated and used.
231 // The latter feature is for creating child terrains for mega-regions.
232 // If there is an existing terrain body, a new
233 // terrain shape is created and added to the body.
234 // This call is most often used to update the heightMap and parameters of the terrain.
235 // (The above does suggest that some simplification/refactoring is in order.)
236 // Called during taint-time.
237 private void UpdateTerrain(uint id, float[] heightMap, Vector3 minCoords, Vector3 maxCoords)
238 {
239 DetailLog("{0},BSTerrainManager.UpdateTerrain,call,id={1},minC={2},maxC={3}",
240 BSScene.DetailLogZero, id, minCoords, maxCoords);
241  
242 // Find high and low points of passed heightmap.
243 // The min and max passed in is usually the area objects can be in (maximum
244 // object height, for instance). The terrain wants the bounding box for the
245 // terrain so replace passed min and max Z with the actual terrain min/max Z.
246 float minZ = float.MaxValue;
247 float maxZ = float.MinValue;
248 foreach (float height in heightMap)
249 {
250 if (height < minZ) minZ = height;
251 if (height > maxZ) maxZ = height;
252 }
253 if (minZ == maxZ)
254 {
255 // If min and max are the same, reduce min a little bit so a good bounding box is created.
256 minZ -= BSTerrainManager.HEIGHT_EQUAL_FUDGE;
257 }
258 minCoords.Z = minZ;
259 maxCoords.Z = maxZ;
260  
261 Vector3 terrainRegionBase = new Vector3(minCoords.X, minCoords.Y, 0f);
262  
263 lock (m_terrains)
264 {
265 BSTerrainPhys terrainPhys;
266 if (m_terrains.TryGetValue(terrainRegionBase, out terrainPhys))
267 {
268 // There is already a terrain in this spot. Free the old and build the new.
269 DetailLog("{0},BSTErrainManager.UpdateTerrain:UpdateExisting,call,id={1},base={2},minC={3},maxC={4}",
270 BSScene.DetailLogZero, id, terrainRegionBase, minCoords, minCoords);
271  
272 // Remove old terrain from the collection
273 m_terrains.Remove(terrainRegionBase);
274 // Release any physical memory it may be using.
275 terrainPhys.Dispose();
276  
277 if (MegaRegionParentPhysicsScene == null)
278 {
279 // This terrain is not part of the mega-region scheme. Create vanilla terrain.
280 BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords);
281 m_terrains.Add(terrainRegionBase, newTerrainPhys);
282  
283 m_terrainModified = true;
284 }
285 else
286 {
287 // It's possible that Combine() was called after this code was queued.
288 // If we are a child of combined regions, we don't create any terrain for us.
289 DetailLog("{0},BSTerrainManager.UpdateTerrain:AmACombineChild,taint", BSScene.DetailLogZero);
290  
291 // Get rid of any terrain that may have been allocated for us.
292 ReleaseGroundPlaneAndTerrain();
293  
294 // I hate doing this, but just bail
295 return;
296 }
297 }
298 else
299 {
300 // We don't know about this terrain so either we are creating a new terrain or
301 // our mega-prim child is giving us a new terrain to add to the phys world
302  
303 // if this is a child terrain, calculate a unique terrain id
304 uint newTerrainID = id;
305 if (newTerrainID >= BSScene.CHILDTERRAIN_ID)
306 newTerrainID = ++m_terrainCount;
307  
308 DetailLog("{0},BSTerrainManager.UpdateTerrain:NewTerrain,taint,newID={1},minCoord={2},maxCoord={3}",
309 BSScene.DetailLogZero, newTerrainID, minCoords, maxCoords);
310 BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords);
311 m_terrains.Add(terrainRegionBase, newTerrainPhys);
312  
313 m_terrainModified = true;
314 }
315 }
316 }
317  
318 // TODO: redo terrain implementation selection to allow other base types than heightMap.
319 private BSTerrainPhys BuildPhysicalTerrain(Vector3 terrainRegionBase, uint id, float[] heightMap, Vector3 minCoords, Vector3 maxCoords)
320 {
321 m_physicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}",
322 LogHeader, m_physicsScene.RegionName, terrainRegionBase,
323 (BSTerrainPhys.TerrainImplementation)BSParam.TerrainImplementation);
324 BSTerrainPhys newTerrainPhys = null;
325 switch ((int)BSParam.TerrainImplementation)
326 {
327 case (int)BSTerrainPhys.TerrainImplementation.Heightmap:
328 newTerrainPhys = new BSTerrainHeightmap(m_physicsScene, terrainRegionBase, id,
329 heightMap, minCoords, maxCoords);
330 break;
331 case (int)BSTerrainPhys.TerrainImplementation.Mesh:
332 newTerrainPhys = new BSTerrainMesh(m_physicsScene, terrainRegionBase, id,
333 heightMap, minCoords, maxCoords);
334 break;
335 default:
336 m_physicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}",
337 LogHeader,
338 (int)BSParam.TerrainImplementation,
339 BSParam.TerrainImplementation,
340 m_physicsScene.RegionName, terrainRegionBase);
341 break;
342 }
343 return newTerrainPhys;
344 }
345  
346 // Return 'true' of this position is somewhere in known physical terrain space
347 public bool IsWithinKnownTerrain(Vector3 pos)
348 {
349 Vector3 terrainBaseXYZ;
350 BSTerrainPhys physTerrain;
351 return GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ);
352 }
353  
354 // Return a new position that is over known terrain if the position is outside our terrain.
355 public Vector3 ClampPositionIntoKnownTerrain(Vector3 pPos)
356 {
357 Vector3 ret = pPos;
358  
359 // First, base addresses are never negative so correct for that possible problem.
360 if (ret.X < 0f || ret.Y < 0f)
361 {
362 ret.X = Util.Clamp<float>(ret.X, 0f, 1000000f);
363 ret.Y = Util.Clamp<float>(ret.Y, 0f, 1000000f);
364 DetailLog("{0},BSTerrainManager.ClampPositionToKnownTerrain,zeroingNegXorY,oldPos={1},newPos={2}",
365 BSScene.DetailLogZero, pPos, ret);
366 }
367  
368 // Can't do this function if we don't know about any terrain.
369 if (m_terrains.Count == 0)
370 return ret;
371  
372 int loopPrevention = 10;
373 Vector3 terrainBaseXYZ;
374 BSTerrainPhys physTerrain;
375 while (!GetTerrainPhysicalAtXYZ(ret, out physTerrain, out terrainBaseXYZ))
376 {
377 // The passed position is not within a known terrain area.
378 // NOTE that GetTerrainPhysicalAtXYZ will set 'terrainBaseXYZ' to the base of the unfound region.
379  
380 // Must be off the top of a region. Find an adjacent region to move into.
381 Vector3 adjacentTerrainBase = FindAdjacentTerrainBase(terrainBaseXYZ);
382  
383 ret.X = Math.Min(ret.X, adjacentTerrainBase.X + (ret.X % DefaultRegionSize.X));
384 ret.Y = Math.Min(ret.Y, adjacentTerrainBase.Y + (ret.X % DefaultRegionSize.Y));
385 DetailLog("{0},BSTerrainManager.ClampPositionToKnownTerrain,findingAdjacentRegion,adjacentRegBase={1},oldPos={2},newPos={3}",
386 BSScene.DetailLogZero, adjacentTerrainBase, pPos, ret);
387  
388 if (loopPrevention-- < 0f)
389 {
390 // The 'while' is a little dangerous so this prevents looping forever if the
391 // mapping of the terrains ever gets messed up (like nothing at <0,0>) or
392 // the list of terrains is in transition.
393 DetailLog("{0},BSTerrainManager.ClampPositionToKnownTerrain,suppressingFindAdjacentRegionLoop", BSScene.DetailLogZero);
394 break;
395 }
396 }
397  
398 return ret;
399 }
400  
401 // Given an X and Y, find the height of the terrain.
402 // Since we could be handling multiple terrains for a mega-region,
403 // the base of the region is calcuated assuming all regions are
404 // the same size and that is the default.
405 // Once the heightMapInfo is found, we have all the information to
406 // compute the offset into the array.
407 private float lastHeightTX = 999999f;
408 private float lastHeightTY = 999999f;
409 private float lastHeight = HEIGHT_INITIAL_LASTHEIGHT;
410 public float GetTerrainHeightAtXYZ(Vector3 pos)
411 {
412 float tX = pos.X;
413 float tY = pos.Y;
414 // You'd be surprized at the number of times this routine is called
415 // with the same parameters as last time.
416 if (!m_terrainModified && (lastHeightTX == tX) && (lastHeightTY == tY))
417 return lastHeight;
418 m_terrainModified = false;
419  
420 lastHeightTX = tX;
421 lastHeightTY = tY;
422 float ret = HEIGHT_GETHEIGHT_RET;
423  
424 Vector3 terrainBaseXYZ;
425 BSTerrainPhys physTerrain;
426 if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ))
427 {
428 ret = physTerrain.GetTerrainHeightAtXYZ(pos - terrainBaseXYZ);
429 }
430 else
431 {
432 m_physicsScene.Logger.ErrorFormat("{0} GetTerrainHeightAtXY: terrain not found: region={1}, x={2}, y={3}",
433 LogHeader, m_physicsScene.RegionName, tX, tY);
434 DetailLog("{0},BSTerrainManager.GetTerrainHeightAtXYZ,terrainNotFound,pos={1},base={2}",
435 BSScene.DetailLogZero, pos, terrainBaseXYZ);
436 }
437  
438 lastHeight = ret;
439 return ret;
440 }
441  
442 public float GetWaterLevelAtXYZ(Vector3 pos)
443 {
444 float ret = WATER_HEIGHT_GETHEIGHT_RET;
445  
446 Vector3 terrainBaseXYZ;
447 BSTerrainPhys physTerrain;
448 if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ))
449 {
450 ret = physTerrain.GetWaterLevelAtXYZ(pos);
451 }
452 else
453 {
454 m_physicsScene.Logger.ErrorFormat("{0} GetWaterHeightAtXY: terrain not found: pos={1}, terrainBase={2}, height={3}",
455 LogHeader, m_physicsScene.RegionName, pos, terrainBaseXYZ, ret);
456 }
457 return ret;
458 }
459  
460 // Given an address, return 'true' of there is a description of that terrain and output
461 // the descriptor class and the 'base' fo the addresses therein.
462 private bool GetTerrainPhysicalAtXYZ(Vector3 pos, out BSTerrainPhys outPhysTerrain, out Vector3 outTerrainBase)
463 {
464 bool ret = false;
465  
466 Vector3 terrainBaseXYZ = Vector3.Zero;
467 if (pos.X < 0f || pos.Y < 0f)
468 {
469 // We don't handle negative addresses so just make up a base that will not be found.
470 terrainBaseXYZ = new Vector3(-DefaultRegionSize.X, -DefaultRegionSize.Y, 0f);
471 }
472 else
473 {
474 int offsetX = ((int)(pos.X / (int)DefaultRegionSize.X)) * (int)DefaultRegionSize.X;
475 int offsetY = ((int)(pos.Y / (int)DefaultRegionSize.Y)) * (int)DefaultRegionSize.Y;
476 terrainBaseXYZ = new Vector3(offsetX, offsetY, 0f);
477 }
478  
479 BSTerrainPhys physTerrain = null;
480 lock (m_terrains)
481 {
482 ret = m_terrains.TryGetValue(terrainBaseXYZ, out physTerrain);
483 }
484 outTerrainBase = terrainBaseXYZ;
485 outPhysTerrain = physTerrain;
486 return ret;
487 }
488  
489 // Given a terrain base, return a terrain base for a terrain that is closer to <0,0> than
490 // this one. Usually used to return an out of bounds object to a known place.
491 private Vector3 FindAdjacentTerrainBase(Vector3 pTerrainBase)
492 {
493 Vector3 ret = pTerrainBase;
494  
495 // Can't do this function if we don't know about any terrain.
496 if (m_terrains.Count == 0)
497 return ret;
498  
499 // Just some sanity
500 ret.X = Util.Clamp<float>(ret.X, 0f, 1000000f);
501 ret.Y = Util.Clamp<float>(ret.Y, 0f, 1000000f);
502 ret.Z = 0f;
503  
504 lock (m_terrains)
505 {
506 // Once down to the <0,0> region, we have to be done.
507 while (ret.X > 0f || ret.Y > 0f)
508 {
509 if (ret.X > 0f)
510 {
511 ret.X = Math.Max(0f, ret.X - DefaultRegionSize.X);
512 DetailLog("{0},BSTerrainManager.FindAdjacentTerrainBase,reducingX,terrainBase={1}", BSScene.DetailLogZero, ret);
513 if (m_terrains.ContainsKey(ret))
514 break;
515 }
516 if (ret.Y > 0f)
517 {
518 ret.Y = Math.Max(0f, ret.Y - DefaultRegionSize.Y);
519 DetailLog("{0},BSTerrainManager.FindAdjacentTerrainBase,reducingY,terrainBase={1}", BSScene.DetailLogZero, ret);
520 if (m_terrains.ContainsKey(ret))
521 break;
522 }
523 }
524 }
525  
526 return ret;
527 }
528  
529 // Although no one seems to check this, I do support combining.
530 public bool SupportsCombining()
531 {
532 return true;
533 }
534  
535 // This routine is called two ways:
536 // One with 'offset' and 'pScene' zero and null but 'extents' giving the maximum
537 // extent of the combined regions. This is to inform the parent of the size
538 // of the combined regions.
539 // and one with 'offset' as the offset of the child region to the base region,
540 // 'pScene' pointing to the parent and 'extents' of zero. This informs the
541 // child of its relative base and new parent.
542 public void Combine(PhysicsScene pScene, Vector3 offset, Vector3 extents)
543 {
544 m_worldOffset = offset;
545 m_worldMax = extents;
546 MegaRegionParentPhysicsScene = pScene;
547 if (pScene != null)
548 {
549 // We are a child.
550 // We want m_worldMax to be the highest coordinate of our piece of terrain.
551 m_worldMax = offset + DefaultRegionSize;
552 }
553 DetailLog("{0},BSTerrainManager.Combine,offset={1},extents={2},wOffset={3},wMax={4}",
554 BSScene.DetailLogZero, offset, extents, m_worldOffset, m_worldMax);
555 }
556  
557 // Unhook all the combining that I know about.
558 public void UnCombine(PhysicsScene pScene)
559 {
560 // Just like ODE, we don't do anything yet.
561 DetailLog("{0},BSTerrainManager.UnCombine", BSScene.DetailLogZero);
562 }
563  
564  
565 private void DetailLog(string msg, params Object[] args)
566 {
567 m_physicsScene.PhysicsLogging.Write(msg, args);
568 }
569 }
570 }