diff options
Diffstat (limited to 'OpenSim.RegionServer/world/World.cs')
-rw-r--r-- | OpenSim.RegionServer/world/World.cs | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/OpenSim.RegionServer/world/World.cs b/OpenSim.RegionServer/world/World.cs new file mode 100644 index 0000000..ba99233 --- /dev/null +++ b/OpenSim.RegionServer/world/World.cs | |||
@@ -0,0 +1,213 @@ | |||
1 | using System; | ||
2 | using libsecondlife; | ||
3 | using libsecondlife.Packets; | ||
4 | using System.Collections.Generic; | ||
5 | using System.Text; | ||
6 | using System.Reflection; | ||
7 | using System.IO; | ||
8 | using OpenSim.Physics.Manager; | ||
9 | using OpenSim.Framework.Interfaces; | ||
10 | using OpenSim.Framework.Assets; | ||
11 | using OpenSim.Framework.Terrain; | ||
12 | |||
13 | namespace OpenSim.world | ||
14 | { | ||
15 | public class World : ILocalStorageReceiver | ||
16 | { | ||
17 | public Dictionary<libsecondlife.LLUUID, Entity> Entities; | ||
18 | public float[] LandMap; | ||
19 | public ScriptEngine Scripts; | ||
20 | public uint _localNumber=0; | ||
21 | private PhysicsScene phyScene; | ||
22 | private float timeStep= 0.1f; | ||
23 | private libsecondlife.TerrainManager TerrainManager; | ||
24 | public ILocalStorage localStorage; | ||
25 | private Random Rand = new Random(); | ||
26 | private uint _primCount = 702000; | ||
27 | private int storageCount; | ||
28 | |||
29 | public World() | ||
30 | { | ||
31 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs - creating new entitities instance"); | ||
32 | Entities = new Dictionary<libsecondlife.LLUUID, Entity>(); | ||
33 | |||
34 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs - creating LandMap"); | ||
35 | TerrainManager = new TerrainManager(new SecondLife()); | ||
36 | Avatar.SetupTemplate("avatar-template.dat"); | ||
37 | // ServerConsole.MainConsole.Instance.WriteLine("World.cs - Creating script engine instance"); | ||
38 | // Initialise this only after the world has loaded | ||
39 | // Scripts = new ScriptEngine(this); | ||
40 | } | ||
41 | |||
42 | public PhysicsScene PhysScene | ||
43 | { | ||
44 | set | ||
45 | { | ||
46 | this.phyScene = value; | ||
47 | } | ||
48 | get | ||
49 | { | ||
50 | return(this.phyScene); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | public void Update() | ||
55 | { | ||
56 | if(this.phyScene.IsThreaded) | ||
57 | { | ||
58 | this.phyScene.GetResults(); | ||
59 | |||
60 | } | ||
61 | |||
62 | foreach (libsecondlife.LLUUID UUID in Entities.Keys) | ||
63 | { | ||
64 | Entities[UUID].addForces(); | ||
65 | } | ||
66 | |||
67 | this.phyScene.Simulate(timeStep); | ||
68 | |||
69 | foreach (libsecondlife.LLUUID UUID in Entities.Keys) | ||
70 | { | ||
71 | Entities[UUID].update(); | ||
72 | } | ||
73 | |||
74 | //backup world data | ||
75 | this.storageCount++; | ||
76 | if(storageCount> 1200) //set to how often you want to backup | ||
77 | { | ||
78 | this.Backup(); | ||
79 | storageCount =0; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | public bool LoadStorageDLL(string dllName) | ||
84 | { | ||
85 | Assembly pluginAssembly = Assembly.LoadFrom(dllName); | ||
86 | ILocalStorage store = null; | ||
87 | |||
88 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
89 | { | ||
90 | if (pluginType.IsPublic) | ||
91 | { | ||
92 | if (!pluginType.IsAbstract) | ||
93 | { | ||
94 | Type typeInterface = pluginType.GetInterface("ILocalStorage", true); | ||
95 | |||
96 | if (typeInterface != null) | ||
97 | { | ||
98 | ILocalStorage plug = (ILocalStorage)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | ||
99 | store = plug; | ||
100 | break; | ||
101 | } | ||
102 | |||
103 | typeInterface = null; | ||
104 | } | ||
105 | } | ||
106 | } | ||
107 | pluginAssembly = null; | ||
108 | this.localStorage = store; | ||
109 | return(store == null); | ||
110 | } | ||
111 | |||
112 | public void RegenerateTerrain() | ||
113 | { | ||
114 | HeightmapGenHills hills = new HeightmapGenHills(); | ||
115 | this.LandMap = hills.GenerateHeightmap(200, 4.0f, 80.0f, false); | ||
116 | this.phyScene.SetTerrain(this.LandMap); | ||
117 | OpenSimRoot.Instance.Cfg.SaveMap(this.LandMap); | ||
118 | |||
119 | foreach(SimClient client in OpenSimRoot.Instance.ClientThreads.Values) { | ||
120 | this.SendLayerData(client); | ||
121 | } | ||
122 | } | ||
123 | public void LoadPrimsFromStorage() | ||
124 | { | ||
125 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs: LoadPrimsFromStorage() - Loading primitives"); | ||
126 | this.localStorage.LoadPrimitives(this); | ||
127 | } | ||
128 | |||
129 | public void PrimFromStorage(PrimData prim) | ||
130 | { | ||
131 | if(prim.LocalID >= this._primCount) | ||
132 | { | ||
133 | _primCount = prim.LocalID + 1; | ||
134 | } | ||
135 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs: PrimFromStorage() - Reloading prim (localId "+ prim.LocalID+ " ) from storage"); | ||
136 | Primitive nPrim = new Primitive(); | ||
137 | nPrim.CreateFromStorage(prim); | ||
138 | this.Entities.Add(nPrim.uuid, nPrim); | ||
139 | } | ||
140 | |||
141 | public void Close() | ||
142 | { | ||
143 | this.localStorage.ShutDown(); | ||
144 | } | ||
145 | |||
146 | public void SendLayerData(SimClient RemoteClient) { | ||
147 | int[] patches = new int[4]; | ||
148 | |||
149 | for (int y = 0; y < 16; y++) | ||
150 | { | ||
151 | for (int x = 0; x < 16; x = x + 4) | ||
152 | { | ||
153 | patches[0] = x + 0 + y * 16; | ||
154 | patches[1] = x + 1 + y * 16; | ||
155 | patches[2] = x + 2 + y * 16; | ||
156 | patches[3] = x + 3 + y * 16; | ||
157 | |||
158 | Packet layerpack = TerrainManager.CreateLandPacket(LandMap, patches); | ||
159 | RemoteClient.OutPacket(layerpack); | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | |||
164 | public void GetInitialPrims(SimClient RemoteClient) | ||
165 | { | ||
166 | foreach (libsecondlife.LLUUID UUID in Entities.Keys) | ||
167 | { | ||
168 | if(Entities[UUID].ToString()== "OpenSim.world.Primitive") | ||
169 | { | ||
170 | ((OpenSim.world.Primitive)Entities[UUID]).UpdateClient(RemoteClient); | ||
171 | } | ||
172 | } | ||
173 | } | ||
174 | |||
175 | public void AddViewerAgent(SimClient AgentClient) { | ||
176 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs:AddViewerAgent() - Creating new avatar for remote viewer agent"); | ||
177 | Avatar NewAvatar = new Avatar(AgentClient); | ||
178 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs:AddViewerAgent() - Adding new avatar to world"); | ||
179 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs:AddViewerAgent() - Starting RegionHandshake "); | ||
180 | NewAvatar.SendRegionHandshake(this); | ||
181 | PhysicsVector pVec = new PhysicsVector(NewAvatar.position.X, NewAvatar.position.Y, NewAvatar.position.Z); | ||
182 | NewAvatar.PhysActor = this.phyScene.AddAvatar(pVec); | ||
183 | this.Entities.Add(AgentClient.AgentID, NewAvatar); | ||
184 | } | ||
185 | |||
186 | public void AddNewPrim(ObjectAddPacket addPacket, SimClient AgentClient) | ||
187 | { | ||
188 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs: AddNewPrim() - Creating new prim"); | ||
189 | Primitive prim = new Primitive(); | ||
190 | prim.CreateFromPacket(addPacket, AgentClient.AgentID, this._primCount); | ||
191 | PhysicsVector pVec = new PhysicsVector(prim.position.X, prim.position.Y, prim.position.Z); | ||
192 | PhysicsVector pSize = new PhysicsVector( 0.255f, 0.255f, 0.255f); | ||
193 | if(OpenSim.world.Avatar.PhysicsEngineFlying) | ||
194 | { | ||
195 | prim.PhysActor = this.phyScene.AddPrim(pVec, pSize ); | ||
196 | } | ||
197 | //prim.PhysicsEnabled = true; | ||
198 | this.Entities.Add(prim.uuid, prim); | ||
199 | this._primCount++; | ||
200 | } | ||
201 | |||
202 | public bool Backup() { | ||
203 | |||
204 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine("World.cs: Backup() - Backing up Primitives"); | ||
205 | foreach (libsecondlife.LLUUID UUID in Entities.Keys) | ||
206 | { | ||
207 | Entities[UUID].BackUp(); | ||
208 | } | ||
209 | return true; | ||
210 | } | ||
211 | |||
212 | } | ||
213 | } | ||