diff options
Diffstat (limited to '')
-rwxr-xr-x | OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs | 271 |
1 files changed, 200 insertions, 71 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs index 23fcfd3..e8040d8 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSTerrainManager.cs | |||
@@ -62,11 +62,12 @@ public abstract class BSTerrainPhys : IDisposable | |||
62 | ID = id; | 62 | ID = id; |
63 | } | 63 | } |
64 | public abstract void Dispose(); | 64 | public abstract void Dispose(); |
65 | public abstract float GetHeightAtXYZ(Vector3 pos); | 65 | public abstract float GetTerrainHeightAtXYZ(Vector3 pos); |
66 | public abstract float GetWaterLevelAtXYZ(Vector3 pos); | ||
66 | } | 67 | } |
67 | 68 | ||
68 | // ========================================================================================== | 69 | // ========================================================================================== |
69 | public sealed class BSTerrainManager | 70 | public sealed class BSTerrainManager : IDisposable |
70 | { | 71 | { |
71 | static string LogHeader = "[BULLETSIM TERRAIN MANAGER]"; | 72 | static string LogHeader = "[BULLETSIM TERRAIN MANAGER]"; |
72 | 73 | ||
@@ -75,13 +76,12 @@ public sealed class BSTerrainManager | |||
75 | public const float HEIGHT_INITIALIZATION = 24.987f; | 76 | public const float HEIGHT_INITIALIZATION = 24.987f; |
76 | public const float HEIGHT_INITIAL_LASTHEIGHT = 24.876f; | 77 | public const float HEIGHT_INITIAL_LASTHEIGHT = 24.876f; |
77 | public const float HEIGHT_GETHEIGHT_RET = 24.765f; | 78 | public const float HEIGHT_GETHEIGHT_RET = 24.765f; |
79 | public const float WATER_HEIGHT_GETHEIGHT_RET = 19.998f; | ||
78 | 80 | ||
79 | // If the min and max height are equal, we reduce the min by this | 81 | // If the min and max height are equal, we reduce the min by this |
80 | // amount to make sure that a bounding box is built for the terrain. | 82 | // amount to make sure that a bounding box is built for the terrain. |
81 | public const float HEIGHT_EQUAL_FUDGE = 0.2f; | 83 | public const float HEIGHT_EQUAL_FUDGE = 0.2f; |
82 | 84 | ||
83 | public const float TERRAIN_COLLISION_MARGIN = 0.0f; | ||
84 | |||
85 | // Until the whole simulator is changed to pass us the region size, we rely on constants. | 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); | 86 | public Vector3 DefaultRegionSize = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionHeight); |
87 | 87 | ||
@@ -122,25 +122,28 @@ public sealed class BSTerrainManager | |||
122 | MegaRegionParentPhysicsScene = null; | 122 | MegaRegionParentPhysicsScene = null; |
123 | } | 123 | } |
124 | 124 | ||
125 | public void Dispose() | ||
126 | { | ||
127 | ReleaseGroundPlaneAndTerrain(); | ||
128 | } | ||
129 | |||
125 | // Create the initial instance of terrain and the underlying ground plane. | 130 | // Create the initial instance of terrain and the underlying ground plane. |
126 | // This is called from the initialization routine so we presume it is | 131 | // This is called from the initialization routine so we presume it is |
127 | // safe to call Bullet in real time. We hope no one is moving prims around yet. | 132 | // safe to call Bullet in real time. We hope no one is moving prims around yet. |
128 | public void CreateInitialGroundPlaneAndTerrain() | 133 | public void CreateInitialGroundPlaneAndTerrain() |
129 | { | 134 | { |
130 | // The ground plane is here to catch things that are trying to drop to negative infinity | 135 | // The ground plane is here to catch things that are trying to drop to negative infinity |
131 | BulletShape groundPlaneShape = new BulletShape( | 136 | BulletShape groundPlaneShape = PhysicsScene.PE.CreateGroundPlaneShape(BSScene.GROUNDPLANE_ID, 1f, BSParam.TerrainCollisionMargin); |
132 | BulletSimAPI.CreateGroundPlaneShape2(BSScene.GROUNDPLANE_ID, 1f, TERRAIN_COLLISION_MARGIN), | 137 | m_groundPlane = PhysicsScene.PE.CreateBodyWithDefaultMotionState(groundPlaneShape, |
133 | BSPhysicsShapeType.SHAPE_GROUNDPLANE); | 138 | BSScene.GROUNDPLANE_ID, Vector3.Zero, Quaternion.Identity); |
134 | m_groundPlane = new BulletBody(BSScene.GROUNDPLANE_ID, | 139 | |
135 | BulletSimAPI.CreateBodyWithDefaultMotionState2(groundPlaneShape.ptr, BSScene.GROUNDPLANE_ID, | 140 | PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, m_groundPlane); |
136 | Vector3.Zero, Quaternion.Identity)); | 141 | PhysicsScene.PE.UpdateSingleAabb(PhysicsScene.World, m_groundPlane); |
137 | BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, m_groundPlane.ptr); | ||
138 | BulletSimAPI.UpdateSingleAabb2(PhysicsScene.World.ptr, m_groundPlane.ptr); | ||
139 | // Ground plane does not move | 142 | // Ground plane does not move |
140 | BulletSimAPI.ForceActivationState2(m_groundPlane.ptr, ActivationState.DISABLE_SIMULATION); | 143 | PhysicsScene.PE.ForceActivationState(m_groundPlane, ActivationState.DISABLE_SIMULATION); |
141 | // Everything collides with the ground plane. | 144 | // Everything collides with the ground plane. |
142 | BulletSimAPI.SetCollisionFilterMask2(m_groundPlane.ptr, | 145 | m_groundPlane.collisionType = CollisionType.Groundplane; |
143 | (uint)CollisionFilterGroups.GroundPlaneFilter, (uint)CollisionFilterGroups.GroundPlaneMask); | 146 | m_groundPlane.ApplyCollisionMask(PhysicsScene); |
144 | 147 | ||
145 | // Build an initial terrain and put it in the world. This quickly gets replaced by the real region terrain. | 148 | // Build an initial terrain and put it in the world. This quickly gets replaced by the real region terrain. |
146 | BSTerrainPhys initialTerrain = new BSTerrainHeightmap(PhysicsScene, Vector3.Zero, BSScene.TERRAIN_ID, DefaultRegionSize); | 149 | BSTerrainPhys initialTerrain = new BSTerrainHeightmap(PhysicsScene, Vector3.Zero, BSScene.TERRAIN_ID, DefaultRegionSize); |
@@ -150,13 +153,13 @@ public sealed class BSTerrainManager | |||
150 | // Release all the terrain structures we might have allocated | 153 | // Release all the terrain structures we might have allocated |
151 | public void ReleaseGroundPlaneAndTerrain() | 154 | public void ReleaseGroundPlaneAndTerrain() |
152 | { | 155 | { |
153 | if (m_groundPlane.ptr != IntPtr.Zero) | 156 | if (m_groundPlane.HasPhysicalBody) |
154 | { | 157 | { |
155 | if (BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, m_groundPlane.ptr)) | 158 | if (PhysicsScene.PE.RemoveObjectFromWorld(PhysicsScene.World, m_groundPlane)) |
156 | { | 159 | { |
157 | BulletSimAPI.DestroyObject2(PhysicsScene.World.ptr, m_groundPlane.ptr); | 160 | PhysicsScene.PE.DestroyObject(PhysicsScene.World, m_groundPlane); |
158 | } | 161 | } |
159 | m_groundPlane.ptr = IntPtr.Zero; | 162 | m_groundPlane.Clear(); |
160 | } | 163 | } |
161 | 164 | ||
162 | ReleaseTerrain(); | 165 | ReleaseTerrain(); |
@@ -165,17 +168,22 @@ public sealed class BSTerrainManager | |||
165 | // Release all the terrain we have allocated | 168 | // Release all the terrain we have allocated |
166 | public void ReleaseTerrain() | 169 | public void ReleaseTerrain() |
167 | { | 170 | { |
168 | foreach (KeyValuePair<Vector3, BSTerrainPhys> kvp in m_terrains) | 171 | lock (m_terrains) |
169 | { | 172 | { |
170 | kvp.Value.Dispose(); | 173 | foreach (KeyValuePair<Vector3, BSTerrainPhys> kvp in m_terrains) |
174 | { | ||
175 | kvp.Value.Dispose(); | ||
176 | } | ||
177 | m_terrains.Clear(); | ||
171 | } | 178 | } |
172 | m_terrains.Clear(); | ||
173 | } | 179 | } |
174 | 180 | ||
175 | // The simulator wants to set a new heightmap for the terrain. | 181 | // The simulator wants to set a new heightmap for the terrain. |
176 | public void SetTerrain(float[] heightMap) { | 182 | public void SetTerrain(float[] heightMap) { |
177 | float[] localHeightMap = heightMap; | 183 | float[] localHeightMap = heightMap; |
178 | PhysicsScene.TaintedObject("TerrainManager.SetTerrain", delegate() | 184 | // If there are multiple requests for changes to the same terrain between ticks, |
185 | // only do that last one. | ||
186 | PhysicsScene.PostTaintObject("TerrainManager.SetTerrain-"+ m_worldOffset.ToString(), 0, delegate() | ||
179 | { | 187 | { |
180 | if (m_worldOffset != Vector3.Zero && MegaRegionParentPhysicsScene != null) | 188 | if (m_worldOffset != Vector3.Zero && MegaRegionParentPhysicsScene != null) |
181 | { | 189 | { |
@@ -211,6 +219,7 @@ public sealed class BSTerrainManager | |||
211 | // terrain shape is created and added to the body. | 219 | // terrain shape is created and added to the body. |
212 | // This call is most often used to update the heightMap and parameters of the terrain. | 220 | // This call is most often used to update the heightMap and parameters of the terrain. |
213 | // (The above does suggest that some simplification/refactoring is in order.) | 221 | // (The above does suggest that some simplification/refactoring is in order.) |
222 | // Called during taint-time. | ||
214 | private void UpdateTerrain(uint id, float[] heightMap, | 223 | private void UpdateTerrain(uint id, float[] heightMap, |
215 | Vector3 minCoords, Vector3 maxCoords, bool inTaintTime) | 224 | Vector3 minCoords, Vector3 maxCoords, bool inTaintTime) |
216 | { | 225 | { |
@@ -220,7 +229,7 @@ public sealed class BSTerrainManager | |||
220 | // Find high and low points of passed heightmap. | 229 | // Find high and low points of passed heightmap. |
221 | // The min and max passed in is usually the area objects can be in (maximum | 230 | // The min and max passed in is usually the area objects can be in (maximum |
222 | // object height, for instance). The terrain wants the bounding box for the | 231 | // object height, for instance). The terrain wants the bounding box for the |
223 | // terrain so we replace passed min and max Z with the actual terrain min/max Z. | 232 | // terrain so replace passed min and max Z with the actual terrain min/max Z. |
224 | float minZ = float.MaxValue; | 233 | float minZ = float.MaxValue; |
225 | float maxZ = float.MinValue; | 234 | float maxZ = float.MinValue; |
226 | foreach (float height in heightMap) | 235 | foreach (float height in heightMap) |
@@ -238,15 +247,15 @@ public sealed class BSTerrainManager | |||
238 | 247 | ||
239 | Vector3 terrainRegionBase = new Vector3(minCoords.X, minCoords.Y, 0f); | 248 | Vector3 terrainRegionBase = new Vector3(minCoords.X, minCoords.Y, 0f); |
240 | 249 | ||
241 | BSTerrainPhys terrainPhys; | 250 | lock (m_terrains) |
242 | if (m_terrains.TryGetValue(terrainRegionBase, out terrainPhys)) | ||
243 | { | 251 | { |
244 | // There is already a terrain in this spot. Free the old and build the new. | 252 | BSTerrainPhys terrainPhys; |
245 | DetailLog("{0},UpdateTerrain:UpdateExisting,call,id={1},base={2},minC={3},maxC={4}", | 253 | if (m_terrains.TryGetValue(terrainRegionBase, out terrainPhys)) |
246 | BSScene.DetailLogZero, id, terrainRegionBase, minCoords, minCoords); | ||
247 | |||
248 | PhysicsScene.TaintedObject(inTaintTime, "BSScene.UpdateTerrain:UpdateExisting", delegate() | ||
249 | { | 254 | { |
255 | // There is already a terrain in this spot. Free the old and build the new. | ||
256 | DetailLog("{0},UpdateTerrain:UpdateExisting,call,id={1},base={2},minC={3},maxC={4}", | ||
257 | BSScene.DetailLogZero, id, terrainRegionBase, minCoords, minCoords); | ||
258 | |||
250 | // Remove old terrain from the collection | 259 | // Remove old terrain from the collection |
251 | m_terrains.Remove(terrainRegionBase); | 260 | m_terrains.Remove(terrainRegionBase); |
252 | // Release any physical memory it may be using. | 261 | // Release any physical memory it may be using. |
@@ -271,35 +280,24 @@ public sealed class BSTerrainManager | |||
271 | // I hate doing this, but just bail | 280 | // I hate doing this, but just bail |
272 | return; | 281 | return; |
273 | } | 282 | } |
274 | }); | 283 | } |
275 | } | 284 | else |
276 | else | 285 | { |
277 | { | 286 | // We don't know about this terrain so either we are creating a new terrain or |
278 | // We don't know about this terrain so either we are creating a new terrain or | 287 | // our mega-prim child is giving us a new terrain to add to the phys world |
279 | // our mega-prim child is giving us a new terrain to add to the phys world | ||
280 | |||
281 | // if this is a child terrain, calculate a unique terrain id | ||
282 | uint newTerrainID = id; | ||
283 | if (newTerrainID >= BSScene.CHILDTERRAIN_ID) | ||
284 | newTerrainID = ++m_terrainCount; | ||
285 | |||
286 | float[] heightMapX = heightMap; | ||
287 | Vector3 minCoordsX = minCoords; | ||
288 | Vector3 maxCoordsX = maxCoords; | ||
289 | 288 | ||
290 | DetailLog("{0},UpdateTerrain:NewTerrain,call,id={1}, minC={2}, maxC={3}", | 289 | // if this is a child terrain, calculate a unique terrain id |
291 | BSScene.DetailLogZero, newTerrainID, minCoords, minCoords); | 290 | uint newTerrainID = id; |
291 | if (newTerrainID >= BSScene.CHILDTERRAIN_ID) | ||
292 | newTerrainID = ++m_terrainCount; | ||
292 | 293 | ||
293 | // Code that must happen at taint-time | 294 | DetailLog("{0},UpdateTerrain:NewTerrain,taint,newID={1},minCoord={2},maxCoord={3}", |
294 | PhysicsScene.TaintedObject(inTaintTime, "BSScene.UpdateTerrain:NewTerrain", delegate() | 295 | BSScene.DetailLogZero, newTerrainID, minCoords, minCoords); |
295 | { | ||
296 | DetailLog("{0},UpdateTerrain:NewTerrain,taint,baseX={1},baseY={2}", | ||
297 | BSScene.DetailLogZero, minCoordsX.X, minCoordsX.Y); | ||
298 | BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords); | 296 | BSTerrainPhys newTerrainPhys = BuildPhysicalTerrain(terrainRegionBase, id, heightMap, minCoords, maxCoords); |
299 | m_terrains.Add(terrainRegionBase, newTerrainPhys); | 297 | m_terrains.Add(terrainRegionBase, newTerrainPhys); |
300 | 298 | ||
301 | m_terrainModified = true; | 299 | m_terrainModified = true; |
302 | }); | 300 | } |
303 | } | 301 | } |
304 | } | 302 | } |
305 | 303 | ||
@@ -308,9 +306,9 @@ public sealed class BSTerrainManager | |||
308 | { | 306 | { |
309 | PhysicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}", | 307 | PhysicsScene.Logger.DebugFormat("{0} Terrain for {1}/{2} created with {3}", |
310 | LogHeader, PhysicsScene.RegionName, terrainRegionBase, | 308 | LogHeader, PhysicsScene.RegionName, terrainRegionBase, |
311 | (BSTerrainPhys.TerrainImplementation)PhysicsScene.Params.terrainImplementation); | 309 | (BSTerrainPhys.TerrainImplementation)BSParam.TerrainImplementation); |
312 | BSTerrainPhys newTerrainPhys = null; | 310 | BSTerrainPhys newTerrainPhys = null; |
313 | switch ((int)PhysicsScene.Params.terrainImplementation) | 311 | switch ((int)BSParam.TerrainImplementation) |
314 | { | 312 | { |
315 | case (int)BSTerrainPhys.TerrainImplementation.Heightmap: | 313 | case (int)BSTerrainPhys.TerrainImplementation.Heightmap: |
316 | newTerrainPhys = new BSTerrainHeightmap(PhysicsScene, terrainRegionBase, id, | 314 | newTerrainPhys = new BSTerrainHeightmap(PhysicsScene, terrainRegionBase, id, |
@@ -323,14 +321,69 @@ public sealed class BSTerrainManager | |||
323 | default: | 321 | default: |
324 | PhysicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}", | 322 | PhysicsScene.Logger.ErrorFormat("{0} Bad terrain implementation specified. Type={1}/{2},Region={3}/{4}", |
325 | LogHeader, | 323 | LogHeader, |
326 | (int)PhysicsScene.Params.terrainImplementation, | 324 | (int)BSParam.TerrainImplementation, |
327 | PhysicsScene.Params.terrainImplementation, | 325 | BSParam.TerrainImplementation, |
328 | PhysicsScene.RegionName, terrainRegionBase); | 326 | PhysicsScene.RegionName, terrainRegionBase); |
329 | break; | 327 | break; |
330 | } | 328 | } |
331 | return newTerrainPhys; | 329 | return newTerrainPhys; |
332 | } | 330 | } |
333 | 331 | ||
332 | // Return 'true' of this position is somewhere in known physical terrain space | ||
333 | public bool IsWithinKnownTerrain(Vector3 pos) | ||
334 | { | ||
335 | Vector3 terrainBaseXYZ; | ||
336 | BSTerrainPhys physTerrain; | ||
337 | return GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ); | ||
338 | } | ||
339 | |||
340 | // Return a new position that is over known terrain if the position is outside our terrain. | ||
341 | public Vector3 ClampPositionIntoKnownTerrain(Vector3 pPos) | ||
342 | { | ||
343 | Vector3 ret = pPos; | ||
344 | |||
345 | // Can't do this function if we don't know about any terrain. | ||
346 | if (m_terrains.Count == 0) | ||
347 | return ret; | ||
348 | |||
349 | int loopPrevention = 5; | ||
350 | Vector3 terrainBaseXYZ; | ||
351 | BSTerrainPhys physTerrain; | ||
352 | while (!GetTerrainPhysicalAtXYZ(ret, out physTerrain, out terrainBaseXYZ)) | ||
353 | { | ||
354 | // The passed position is not within a known terrain area. | ||
355 | |||
356 | // First, base addresses are never negative so correct for that possible problem. | ||
357 | if (ret.X < 0f || ret.Y < 0f) | ||
358 | { | ||
359 | if (ret.X < 0f) | ||
360 | ret.X = 0f; | ||
361 | if (ret.Y < 0f) | ||
362 | ret.Y = 0f; | ||
363 | DetailLog("{0},BSTerrainManager.ClampPositionToKnownTerrain,zeroingNegXorY,oldPos={1},newPos={2}", | ||
364 | BSScene.DetailLogZero, pPos, ret); | ||
365 | } | ||
366 | else | ||
367 | { | ||
368 | // Must be off the top of a region. Find an adjacent region to move into. | ||
369 | Vector3 adjacentTerrainBase = FindAdjacentTerrainBase(terrainBaseXYZ); | ||
370 | |||
371 | ret.X = Math.Min(ret.X, adjacentTerrainBase.X + DefaultRegionSize.X); | ||
372 | ret.Y = Math.Min(ret.Y, adjacentTerrainBase.Y + DefaultRegionSize.Y); | ||
373 | DetailLog("{0},BSTerrainManager.ClampPositionToKnownTerrain,findingAdjacentRegion,adjacentRegBase={1},oldPos={2},newPos={3}", | ||
374 | BSScene.DetailLogZero, adjacentTerrainBase, pPos, ret); | ||
375 | } | ||
376 | if (loopPrevention-- < 0f) | ||
377 | { | ||
378 | // The 'while' is a little dangerous so this prevents looping forever if the | ||
379 | // mapping of the terrains ever gets messed up (like nothing at <0,0>) or | ||
380 | // the list of terrains is in transition. | ||
381 | DetailLog("{0},BSTerrainManager.ClampPositionToKnownTerrain,suppressingFindAdjacentRegionLoop", BSScene.DetailLogZero); | ||
382 | break; | ||
383 | } | ||
384 | } | ||
385 | return ret; | ||
386 | } | ||
334 | 387 | ||
335 | // Given an X and Y, find the height of the terrain. | 388 | // Given an X and Y, find the height of the terrain. |
336 | // Since we could be handling multiple terrains for a mega-region, | 389 | // Since we could be handling multiple terrains for a mega-region, |
@@ -341,40 +394,116 @@ public sealed class BSTerrainManager | |||
341 | private float lastHeightTX = 999999f; | 394 | private float lastHeightTX = 999999f; |
342 | private float lastHeightTY = 999999f; | 395 | private float lastHeightTY = 999999f; |
343 | private float lastHeight = HEIGHT_INITIAL_LASTHEIGHT; | 396 | private float lastHeight = HEIGHT_INITIAL_LASTHEIGHT; |
344 | public float GetTerrainHeightAtXYZ(Vector3 loc) | 397 | public float GetTerrainHeightAtXYZ(Vector3 pos) |
345 | { | 398 | { |
346 | float tX = loc.X; | 399 | float tX = pos.X; |
347 | float tY = loc.Y; | 400 | float tY = pos.Y; |
348 | // You'd be surprized at the number of times this routine is called | 401 | // You'd be surprized at the number of times this routine is called |
349 | // with the same parameters as last time. | 402 | // with the same parameters as last time. |
350 | if (!m_terrainModified && lastHeightTX == tX && lastHeightTY == tY) | 403 | if (!m_terrainModified && (lastHeightTX == tX) && (lastHeightTY == tY)) |
351 | return lastHeight; | 404 | return lastHeight; |
405 | m_terrainModified = false; | ||
352 | 406 | ||
353 | lastHeightTX = tX; | 407 | lastHeightTX = tX; |
354 | lastHeightTY = tY; | 408 | lastHeightTY = tY; |
355 | float ret = HEIGHT_GETHEIGHT_RET; | 409 | float ret = HEIGHT_GETHEIGHT_RET; |
356 | 410 | ||
357 | int offsetX = ((int)(tX / (int)DefaultRegionSize.X)) * (int)DefaultRegionSize.X; | 411 | Vector3 terrainBaseXYZ; |
358 | int offsetY = ((int)(tY / (int)DefaultRegionSize.Y)) * (int)DefaultRegionSize.Y; | ||
359 | Vector3 terrainBaseXYZ = new Vector3(offsetX, offsetY, 0f); | ||
360 | |||
361 | BSTerrainPhys physTerrain; | 412 | BSTerrainPhys physTerrain; |
362 | if (m_terrains.TryGetValue(terrainBaseXYZ, out physTerrain)) | 413 | if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ)) |
363 | { | 414 | { |
364 | ret = physTerrain.GetHeightAtXYZ(loc - terrainBaseXYZ); | 415 | ret = physTerrain.GetTerrainHeightAtXYZ(pos - terrainBaseXYZ); |
365 | DetailLog("{0},BSTerrainManager.GetTerrainHeightAtXYZ,loc={1},base={2},height={3}", | ||
366 | BSScene.DetailLogZero, loc, terrainBaseXYZ, ret); | ||
367 | } | 416 | } |
368 | else | 417 | else |
369 | { | 418 | { |
370 | PhysicsScene.Logger.ErrorFormat("{0} GetTerrainHeightAtXY: terrain not found: region={1}, x={2}, y={3}", | 419 | PhysicsScene.Logger.ErrorFormat("{0} GetTerrainHeightAtXY: terrain not found: region={1}, x={2}, y={3}", |
371 | LogHeader, PhysicsScene.RegionName, tX, tY); | 420 | LogHeader, PhysicsScene.RegionName, tX, tY); |
421 | DetailLog("{0},BSTerrainManager.GetTerrainHeightAtXYZ,terrainNotFound,pos={1},base={2}", | ||
422 | BSScene.DetailLogZero, pos, terrainBaseXYZ); | ||
372 | } | 423 | } |
373 | m_terrainModified = false; | 424 | |
374 | lastHeight = ret; | 425 | lastHeight = ret; |
375 | return ret; | 426 | return ret; |
376 | } | 427 | } |
377 | 428 | ||
429 | public float GetWaterLevelAtXYZ(Vector3 pos) | ||
430 | { | ||
431 | float ret = WATER_HEIGHT_GETHEIGHT_RET; | ||
432 | |||
433 | Vector3 terrainBaseXYZ; | ||
434 | BSTerrainPhys physTerrain; | ||
435 | if (GetTerrainPhysicalAtXYZ(pos, out physTerrain, out terrainBaseXYZ)) | ||
436 | { | ||
437 | ret = physTerrain.GetWaterLevelAtXYZ(pos); | ||
438 | } | ||
439 | else | ||
440 | { | ||
441 | PhysicsScene.Logger.ErrorFormat("{0} GetWaterHeightAtXY: terrain not found: pos={1}, terrainBase={2}, height={3}", | ||
442 | LogHeader, PhysicsScene.RegionName, pos, terrainBaseXYZ, ret); | ||
443 | } | ||
444 | return ret; | ||
445 | } | ||
446 | |||
447 | // Given an address, return 'true' of there is a description of that terrain and output | ||
448 | // the descriptor class and the 'base' fo the addresses therein. | ||
449 | private bool GetTerrainPhysicalAtXYZ(Vector3 pos, out BSTerrainPhys outPhysTerrain, out Vector3 outTerrainBase) | ||
450 | { | ||
451 | bool ret = false; | ||
452 | |||
453 | Vector3 terrainBaseXYZ = Vector3.Zero; | ||
454 | if (pos.X < 0f || pos.Y < 0f) | ||
455 | { | ||
456 | // We don't handle negative addresses so just make up a base that will not be found. | ||
457 | terrainBaseXYZ = new Vector3(-DefaultRegionSize.X, -DefaultRegionSize.Y, 0f); | ||
458 | } | ||
459 | else | ||
460 | { | ||
461 | int offsetX = ((int)(pos.X / (int)DefaultRegionSize.X)) * (int)DefaultRegionSize.X; | ||
462 | int offsetY = ((int)(pos.Y / (int)DefaultRegionSize.Y)) * (int)DefaultRegionSize.Y; | ||
463 | terrainBaseXYZ = new Vector3(offsetX, offsetY, 0f); | ||
464 | } | ||
465 | |||
466 | BSTerrainPhys physTerrain = null; | ||
467 | lock (m_terrains) | ||
468 | { | ||
469 | ret = m_terrains.TryGetValue(terrainBaseXYZ, out physTerrain); | ||
470 | } | ||
471 | outTerrainBase = terrainBaseXYZ; | ||
472 | outPhysTerrain = physTerrain; | ||
473 | return ret; | ||
474 | } | ||
475 | |||
476 | // Given a terrain base, return a terrain base for a terrain that is closer to <0,0> than | ||
477 | // this one. Usually used to return an out of bounds object to a known place. | ||
478 | private Vector3 FindAdjacentTerrainBase(Vector3 pTerrainBase) | ||
479 | { | ||
480 | Vector3 ret = pTerrainBase; | ||
481 | ret.Z = 0f; | ||
482 | lock (m_terrains) | ||
483 | { | ||
484 | // Once down to the <0,0> region, we have to be done. | ||
485 | while (ret.X > 0f && ret.Y > 0f) | ||
486 | { | ||
487 | if (ret.X > 0f) | ||
488 | { | ||
489 | ret.X = Math.Max(0f, ret.X - DefaultRegionSize.X); | ||
490 | DetailLog("{0},BSTerrainManager.FindAdjacentTerrainBase,reducingX,terrainBase={1}", BSScene.DetailLogZero, ret); | ||
491 | if (m_terrains.ContainsKey(ret)) | ||
492 | break; | ||
493 | } | ||
494 | if (ret.Y > 0f) | ||
495 | { | ||
496 | ret.Y = Math.Max(0f, ret.Y - DefaultRegionSize.Y); | ||
497 | DetailLog("{0},BSTerrainManager.FindAdjacentTerrainBase,reducingY,terrainBase={1}", BSScene.DetailLogZero, ret); | ||
498 | if (m_terrains.ContainsKey(ret)) | ||
499 | break; | ||
500 | } | ||
501 | } | ||
502 | } | ||
503 | |||
504 | return ret; | ||
505 | } | ||
506 | |||
378 | // Although no one seems to check this, I do support combining. | 507 | // Although no one seems to check this, I do support combining. |
379 | public bool SupportsCombining() | 508 | public bool SupportsCombining() |
380 | { | 509 | { |