From aae0222f8154a32d71112a8a468412efa5b857da Mon Sep 17 00:00:00 2001 From: MW Date: Thu, 17 May 2007 10:24:10 +0000 Subject: More OO and abstract goodness for Gareth to drool over. Cleaned up the World class a bit and added a WorldBase class (that World now inherits from) --- OpenSim.RegionServer/world/World.cs | 302 ++++++++++++++---------------------- 1 file changed, 119 insertions(+), 183 deletions(-) (limited to 'OpenSim.RegionServer/world/World.cs') diff --git a/OpenSim.RegionServer/world/World.cs b/OpenSim.RegionServer/world/World.cs index 640ab81..72214d4 100644 --- a/OpenSim.RegionServer/world/World.cs +++ b/OpenSim.RegionServer/world/World.cs @@ -18,33 +18,39 @@ using OpenSim.Terrain; namespace OpenSim.world { - public partial class World : ILocalStorageReceiver, IScriptAPI + public partial class World : WorldBase, ILocalStorageReceiver, IScriptAPI { public object LockPhysicsEngine = new object(); - public Dictionary Entities; public Dictionary Avatars; public Dictionary Prims; //public ScriptEngine Scripts; - public TerrainEngine Terrain; //TODO: Replace TerrainManager with this. public uint _localNumber = 0; private PhysicsScene phyScene; private float timeStep = 0.1f; - private libsecondlife.TerrainManager TerrainManager; // To be referenced via TerrainEngine public ILocalStorage localStorage; private Random Rand = new Random(); private uint _primCount = 702000; private int storageCount; - private Dictionary m_clientThreads; private Dictionary m_scriptHandlers; private Dictionary m_scripts; - private ulong m_regionHandle; - private string m_regionName; - private InventoryCache _inventoryCache; - private AssetCache _assetCache; private Mutex updateLock; - private RegionInfo m_regInfo; public string m_datastore; + #region Properties + public PhysicsScene PhysScene + { + set + { + this.phyScene = value; + } + get + { + return (this.phyScene); + } + } + #endregion + + #region Constructors /// /// Creates a new World class, and a region to go with it. /// @@ -85,7 +91,9 @@ namespace OpenSim.world OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.CRITICAL,"World.cs: Constructor failed with exception " + e.ToString()); } } + #endregion + #region Script Methods /// /// Loads a new script into the specified entity /// @@ -156,37 +164,13 @@ namespace OpenSim.world } } - public InventoryCache InventoryCache - { - set - { - this._inventoryCache = value; - } - } - - public AssetCache AssetCache - { - set - { - this._assetCache = value; - } - } - public PhysicsScene PhysScene - { - set - { - this.phyScene = value; - } - get - { - return (this.phyScene); - } - } + #endregion + #region Update Methods /// /// Performs per-frame updates on the world, this should be the central world loop /// - public void Update() + public override void Update() { updateLock.WaitOne(); try @@ -235,6 +219,40 @@ namespace OpenSim.world updateLock.ReleaseMutex(); } + public bool Backup() + { + try + { + // Terrain backup routines + if (Terrain.tainted > 0) + { + Terrain.tainted = 0; + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: Backup() - Terrain tainted, saving."); + localStorage.SaveMap(Terrain.getHeights1D()); + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: Backup() - Terrain saved, informing Physics."); + phyScene.SetTerrain(Terrain.getHeights1D()); + } + + // Primitive backup routines + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: Backup() - Backing up Primitives"); + foreach (libsecondlife.LLUUID UUID in Entities.Keys) + { + Entities[UUID].BackUp(); + } + + // Backup successful + return true; + } + catch (Exception e) + { + // Backup failed + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "World.cs: Backup() - Backup Failed with exception " + e.ToString()); + return false; + } + } + #endregion + + #region Setup Methods /// /// Loads a new storage subsystem from a named library /// @@ -279,6 +297,16 @@ namespace OpenSim.world } } + public void SetDefaultScripts() + { + this.m_scripts.Add("FollowRandomAvatar", delegate() + { + return new OpenSim.RegionServer.world.scripting.FollowRandomAvatar(); + }); + } + + #endregion + #region Regenerate Terrain /// @@ -375,10 +403,11 @@ namespace OpenSim.world #endregion + #region Load Terrain /// /// Loads the World heightmap /// - public void LoadWorldMap() + public override void LoadWorldMap() { try { @@ -400,6 +429,32 @@ namespace OpenSim.world OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM,"World.cs: LoadWorldMap() - Failed with exception " + e.ToString()); } } + #endregion + + #region Primitives Methods + + /// + /// Sends prims to a client + /// + /// Client to send to + public void GetInitialPrims(SimClient RemoteClient) + { + try + { + foreach (libsecondlife.LLUUID UUID in Entities.Keys) + { + if (Entities[UUID] is Primitive) + { + Primitive primitive = Entities[UUID] as Primitive; + primitive.UpdateClient(RemoteClient); + } + } + } + catch (Exception e) + { + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: GetInitialPrims() - Failed with exception " + e.ToString()); + } + } /// /// Loads the World's objects @@ -440,108 +495,37 @@ namespace OpenSim.world } } - /// - /// Tidy before shutdown - /// - public void Close() - { - try - { - this.localStorage.ShutDown(); - } - catch (Exception e) - { - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH,"World.cs: Close() - Failed with exception " + e.ToString()); - } - } - - /// - /// Send the region heightmap to the client - /// - /// Client to send to - public void SendLayerData(SimClient RemoteClient) + public void AddNewPrim(ObjectAddPacket addPacket, SimClient AgentClient) { try { - int[] patches = new int[4]; - - for (int y = 0; y < 16; y++) + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: AddNewPrim() - Creating new prim"); + Primitive prim = new Primitive(m_clientThreads, m_regionHandle, this); + prim.CreateFromPacket(addPacket, AgentClient.AgentID, this._primCount); + PhysicsVector pVec = new PhysicsVector(prim.Pos.X, prim.Pos.Y, prim.Pos.Z); + PhysicsVector pSize = new PhysicsVector(0.255f, 0.255f, 0.255f); + if (OpenSim.world.Avatar.PhysicsEngineFlying) { - for (int x = 0; x < 16; x = x + 4) + lock (this.LockPhysicsEngine) { - patches[0] = x + 0 + y * 16; - patches[1] = x + 1 + y * 16; - patches[2] = x + 2 + y * 16; - patches[3] = x + 3 + y * 16; - - Packet layerpack = TerrainManager.CreateLandPacket(Terrain.getHeights1D(), patches); - RemoteClient.OutPacket(layerpack); + prim.PhysActor = this.phyScene.AddPrim(pVec, pSize); } } - } - catch (Exception e) - { - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM,"World.cs: SendLayerData() - Failed with exception " + e.ToString()); - } - } - - /// - /// Sends a specified patch to a client - /// - /// Patch coordinate (x) 0..16 - /// Patch coordinate (y) 0..16 - /// The client to send to - public void SendLayerData(int px, int py, SimClient RemoteClient) - { - try - { - int[] patches = new int[1]; - int patchx, patchy; - patchx = px / 16; - /* if (patchx > 12) - { - patchx = 12; - }*/ - patchy = py / 16; - patches[0] = patchx + 0 + patchy * 16; - //patches[1] = patchx + 1 + patchy * 16; - //patches[2] = patchx + 2 + patchy * 16; - //patches[3] = patchx + 3 + patchy * 16; - - Packet layerpack = TerrainManager.CreateLandPacket(Terrain.getHeights1D(), patches); - RemoteClient.OutPacket(layerpack); + this.Entities.Add(prim.uuid, prim); + this._primCount++; } catch (Exception e) { - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM,"World.cs: SendLayerData() - Failed with exception " + e.ToString()); + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: AddNewPrim() - Failed with exception " + e.ToString()); } } - /// - /// Sends prims to a client - /// - /// Client to send to - public void GetInitialPrims(SimClient RemoteClient) - { - try - { - foreach (libsecondlife.LLUUID UUID in Entities.Keys) - { - if (Entities[UUID] is Primitive) - { - Primitive primitive = Entities[UUID] as Primitive; - primitive.UpdateClient(RemoteClient); - } - } - } - catch (Exception e) - { - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM,"World.cs: GetInitialPrims() - Failed with exception " + e.ToString()); - } - } + #endregion + + #region Add/Remove Avatar Methods - public void AddViewerAgent(SimClient agentClient) + public override void AddViewerAgent(SimClient agentClient) { try { @@ -587,7 +571,7 @@ namespace OpenSim.world } } - public void RemoveViewerAgent(SimClient agentClient) + public override void RemoveViewerAgent(SimClient agentClient) { try { @@ -609,72 +593,24 @@ namespace OpenSim.world OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM,"World.cs: RemoveViewerAgent() - Failed with exception " + e.ToString()); } } + #endregion - public void AddNewPrim(ObjectAddPacket addPacket, SimClient AgentClient) - { - try - { - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"World.cs: AddNewPrim() - Creating new prim"); - Primitive prim = new Primitive(m_clientThreads, m_regionHandle, this); - prim.CreateFromPacket(addPacket, AgentClient.AgentID, this._primCount); - PhysicsVector pVec = new PhysicsVector(prim.Pos.X, prim.Pos.Y, prim.Pos.Z); - PhysicsVector pSize = new PhysicsVector(0.255f, 0.255f, 0.255f); - if (OpenSim.world.Avatar.PhysicsEngineFlying) - { - lock (this.LockPhysicsEngine) - { - prim.PhysActor = this.phyScene.AddPrim(pVec, pSize); - } - } - - this.Entities.Add(prim.uuid, prim); - this._primCount++; - } - catch (Exception e) - { - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM,"World.cs: AddNewPrim() - Failed with exception " + e.ToString()); - } - } - - public bool Backup() + #region ShutDown + /// + /// Tidy before shutdown + /// + public override void Close() { try { - // Terrain backup routines - if (Terrain.tainted > 0) - { - Terrain.tainted = 0; - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"World.cs: Backup() - Terrain tainted, saving."); - localStorage.SaveMap(Terrain.getHeights1D()); - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"World.cs: Backup() - Terrain saved, informing Physics."); - phyScene.SetTerrain(Terrain.getHeights1D()); - } - - // Primitive backup routines - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"World.cs: Backup() - Backing up Primitives"); - foreach (libsecondlife.LLUUID UUID in Entities.Keys) - { - Entities[UUID].BackUp(); - } - - // Backup successful - return true; + this.localStorage.ShutDown(); } catch (Exception e) { - // Backup failed - OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH,"World.cs: Backup() - Backup Failed with exception " + e.ToString()); - return false; + OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "World.cs: Close() - Failed with exception " + e.ToString()); } } - - public void SetDefaultScripts() - { - this.m_scripts.Add("FollowRandomAvatar", delegate() - { - return new OpenSim.RegionServer.world.scripting.FollowRandomAvatar(); - }); - } + #endregion } } -- cgit v1.1