World.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 System.Threading;
  9. using System.Timers;
  10. using OpenSim.Physics.Manager;
  11. using OpenSim.Framework.Interfaces;
  12. using OpenSim.Framework.Types;
  13. using OpenSim.Framework.Inventory;
  14. using OpenSim.Framework;
  15. using OpenSim.RegionServer.world.scripting;
  16. using OpenSim.Terrain;
  17. using OpenGrid.Framework.Communications;
  18. namespace OpenSim.world
  19. {
  20. public partial class World : WorldBase, ILocalStorageReceiver, IScriptAPI
  21. {
  22. protected System.Timers.Timer m_heartbeatTimer = new System.Timers.Timer();
  23. public object LockPhysicsEngine = new object();
  24. public Dictionary<libsecondlife.LLUUID, Avatar> Avatars;
  25. public Dictionary<libsecondlife.LLUUID, Primitive> Prims;
  26. //public ScriptEngine Scripts;
  27. public uint _localNumber = 0;
  28. private PhysicsScene phyScene;
  29. private float timeStep = 0.1f;
  30. public ILocalStorage localStorage;
  31. private Random Rand = new Random();
  32. private uint _primCount = 702000;
  33. private int storageCount;
  34. private Dictionary<LLUUID, ScriptHandler> m_scriptHandlers;
  35. private Dictionary<string, ScriptFactory> m_scripts;
  36. private Mutex updateLock;
  37. public string m_datastore;
  38. protected AuthenticateSessionsBase authenticateHandler;
  39. protected RegionCommsHostBase regionCommsHost;
  40. protected RegionServerCommsManager commsManager;
  41. #region Properties
  42. /// <summary>
  43. ///
  44. /// </summary>
  45. public PhysicsScene PhysScene
  46. {
  47. set
  48. {
  49. this.phyScene = value;
  50. }
  51. get
  52. {
  53. return (this.phyScene);
  54. }
  55. }
  56. #endregion
  57. #region Constructors
  58. /// <summary>
  59. /// Creates a new World class, and a region to go with it.
  60. /// </summary>
  61. /// <param name="clientThreads">Dictionary to contain client threads</param>
  62. /// <param name="regionHandle">Region Handle for this region</param>
  63. /// <param name="regionName">Region Name for this region</param>
  64. public World(Dictionary<uint, IClientAPI> clientThreads, RegionInfo regInfo, AuthenticateSessionsBase authen, RegionServerCommsManager commsMan)
  65. {
  66. try
  67. {
  68. updateLock = new Mutex(false);
  69. this.authenticateHandler = authen;
  70. this.commsManager = commsMan;
  71. m_clientThreads = clientThreads;
  72. m_regInfo = regInfo;
  73. m_regionHandle = m_regInfo.RegionHandle;
  74. m_regionName = m_regInfo.RegionName;
  75. this.m_datastore = m_regInfo.DataStore;
  76. this.RegisterRegionWithComms();
  77. m_scriptHandlers = new Dictionary<LLUUID, ScriptHandler>();
  78. m_scripts = new Dictionary<string, ScriptFactory>();
  79. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs - creating new entitities instance");
  80. Entities = new Dictionary<libsecondlife.LLUUID, Entity>();
  81. Avatars = new Dictionary<LLUUID, Avatar>();
  82. Prims = new Dictionary<LLUUID, Primitive>();
  83. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs - creating LandMap");
  84. TerrainManager = new TerrainManager(new SecondLife());
  85. Terrain = new TerrainEngine();
  86. Avatar.SetupTemplate("avatar-texture.dat");
  87. Avatar.LoadAnims();
  88. //this.SetDefaultScripts();
  89. //this.LoadScriptEngines();
  90. }
  91. catch (Exception e)
  92. {
  93. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.CRITICAL, "World.cs: Constructor failed with exception " + e.ToString());
  94. }
  95. }
  96. #endregion
  97. /// <summary>
  98. ///
  99. /// </summary>
  100. public void StartTimer()
  101. {
  102. m_heartbeatTimer.Enabled = true;
  103. m_heartbeatTimer.Interval = 100;
  104. m_heartbeatTimer.Elapsed += new ElapsedEventHandler(this.Heartbeat);
  105. }
  106. #region Update Methods
  107. /// <summary>
  108. /// Performs per-frame updates regularly
  109. /// </summary>
  110. /// <param name="sender"></param>
  111. /// <param name="e"></param>
  112. void Heartbeat(object sender, System.EventArgs e)
  113. {
  114. this.Update();
  115. }
  116. /// <summary>
  117. /// Performs per-frame updates on the world, this should be the central world loop
  118. /// </summary>
  119. public override void Update()
  120. {
  121. updateLock.WaitOne();
  122. try
  123. {
  124. if (this.phyScene.IsThreaded)
  125. {
  126. this.phyScene.GetResults();
  127. }
  128. foreach (libsecondlife.LLUUID UUID in Entities.Keys)
  129. {
  130. Entities[UUID].addForces();
  131. }
  132. lock (this.LockPhysicsEngine)
  133. {
  134. this.phyScene.Simulate(timeStep);
  135. }
  136. foreach (libsecondlife.LLUUID UUID in Entities.Keys)
  137. {
  138. Entities[UUID].update();
  139. }
  140. foreach (ScriptHandler scriptHandler in m_scriptHandlers.Values)
  141. {
  142. scriptHandler.OnFrame();
  143. }
  144. foreach (IScriptEngine scripteng in this.scriptEngines.Values)
  145. {
  146. scripteng.OnFrame();
  147. }
  148. //backup world data
  149. this.storageCount++;
  150. if (storageCount > 1200) //set to how often you want to backup
  151. {
  152. this.Backup();
  153. storageCount = 0;
  154. }
  155. }
  156. catch (Exception e)
  157. {
  158. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: Update() - Failed with exception " + e.ToString());
  159. }
  160. updateLock.ReleaseMutex();
  161. }
  162. /// <summary>
  163. ///
  164. /// </summary>
  165. /// <returns></returns>
  166. public bool Backup()
  167. {
  168. try
  169. {
  170. // Terrain backup routines
  171. if (Terrain.tainted > 0)
  172. {
  173. Terrain.tainted = 0;
  174. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: Backup() - Terrain tainted, saving.");
  175. localStorage.SaveMap(Terrain.getHeights1D());
  176. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: Backup() - Terrain saved, informing Physics.");
  177. lock (this.LockPhysicsEngine)
  178. {
  179. phyScene.SetTerrain(Terrain.getHeights1D());
  180. }
  181. }
  182. // Primitive backup routines
  183. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: Backup() - Backing up Primitives");
  184. foreach (libsecondlife.LLUUID UUID in Entities.Keys)
  185. {
  186. Entities[UUID].BackUp();
  187. }
  188. // Backup successful
  189. return true;
  190. }
  191. catch (Exception e)
  192. {
  193. // Backup failed
  194. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "World.cs: Backup() - Backup Failed with exception " + e.ToString());
  195. return false;
  196. }
  197. }
  198. #endregion
  199. #region Setup Methods
  200. /// <summary>
  201. /// Loads a new storage subsystem from a named library
  202. /// </summary>
  203. /// <param name="dllName">Storage Library</param>
  204. /// <returns>Successful or not</returns>
  205. public bool LoadStorageDLL(string dllName)
  206. {
  207. try
  208. {
  209. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  210. ILocalStorage store = null;
  211. foreach (Type pluginType in pluginAssembly.GetTypes())
  212. {
  213. if (pluginType.IsPublic)
  214. {
  215. if (!pluginType.IsAbstract)
  216. {
  217. Type typeInterface = pluginType.GetInterface("ILocalStorage", true);
  218. if (typeInterface != null)
  219. {
  220. ILocalStorage plug = (ILocalStorage)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  221. store = plug;
  222. store.Initialise(this.m_datastore);
  223. break;
  224. }
  225. typeInterface = null;
  226. }
  227. }
  228. }
  229. pluginAssembly = null;
  230. this.localStorage = store;
  231. return (store == null);
  232. }
  233. catch (Exception e)
  234. {
  235. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: LoadStorageDLL() - Failed with exception " + e.ToString());
  236. return false;
  237. }
  238. }
  239. #endregion
  240. #region Regenerate Terrain
  241. /// <summary>
  242. /// Rebuilds the terrain using a procedural algorithm
  243. /// </summary>
  244. public void RegenerateTerrain()
  245. {
  246. try
  247. {
  248. Terrain.hills();
  249. lock (this.LockPhysicsEngine)
  250. {
  251. this.phyScene.SetTerrain(Terrain.getHeights1D());
  252. }
  253. this.localStorage.SaveMap(this.Terrain.getHeights1D());
  254. foreach (IClientAPI client in m_clientThreads.Values)
  255. {
  256. this.SendLayerData(client);
  257. }
  258. foreach (libsecondlife.LLUUID UUID in Entities.Keys)
  259. {
  260. Entities[UUID].LandRenegerated();
  261. }
  262. }
  263. catch (Exception e)
  264. {
  265. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: RegenerateTerrain() - Failed with exception " + e.ToString());
  266. }
  267. }
  268. /// <summary>
  269. /// Rebuilds the terrain using a 2D float array
  270. /// </summary>
  271. /// <param name="newMap">256,256 float array containing heights</param>
  272. public void RegenerateTerrain(float[,] newMap)
  273. {
  274. try
  275. {
  276. this.Terrain.setHeights2D(newMap);
  277. lock (this.LockPhysicsEngine)
  278. {
  279. this.phyScene.SetTerrain(this.Terrain.getHeights1D());
  280. }
  281. this.localStorage.SaveMap(this.Terrain.getHeights1D());
  282. foreach (IClientAPI client in m_clientThreads.Values)
  283. {
  284. this.SendLayerData(client);
  285. }
  286. foreach (libsecondlife.LLUUID UUID in Entities.Keys)
  287. {
  288. Entities[UUID].LandRenegerated();
  289. }
  290. }
  291. catch (Exception e)
  292. {
  293. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: RegenerateTerrain() - Failed with exception " + e.ToString());
  294. }
  295. }
  296. /// <summary>
  297. /// Rebuilds the terrain assuming changes occured at a specified point[?]
  298. /// </summary>
  299. /// <param name="changes">???</param>
  300. /// <param name="pointx">???</param>
  301. /// <param name="pointy">???</param>
  302. public void RegenerateTerrain(bool changes, int pointx, int pointy)
  303. {
  304. try
  305. {
  306. if (changes)
  307. {
  308. /* Dont save here, rely on tainting system instead */
  309. foreach (IClientAPI client in m_clientThreads.Values)
  310. {
  311. this.SendLayerData(pointx, pointy, client);
  312. }
  313. }
  314. }
  315. catch (Exception e)
  316. {
  317. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: RegenerateTerrain() - Failed with exception " + e.ToString());
  318. }
  319. }
  320. #endregion
  321. #region Load Terrain
  322. /// <summary>
  323. /// Loads the World heightmap
  324. /// </summary>
  325. public override void LoadWorldMap()
  326. {
  327. try
  328. {
  329. float[] map = this.localStorage.LoadWorld();
  330. if (map == null)
  331. {
  332. Console.WriteLine("creating new terrain");
  333. this.Terrain.hills();
  334. this.localStorage.SaveMap(this.Terrain.getHeights1D());
  335. }
  336. else
  337. {
  338. this.Terrain.setHeights1D(map);
  339. }
  340. }
  341. catch (Exception e)
  342. {
  343. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: LoadWorldMap() - Failed with exception " + e.ToString());
  344. }
  345. }
  346. #endregion
  347. #region Primitives Methods
  348. /// <summary>
  349. /// Loads the World's objects
  350. /// </summary>
  351. public void LoadPrimsFromStorage()
  352. {
  353. try
  354. {
  355. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs: LoadPrimsFromStorage() - Loading primitives");
  356. this.localStorage.LoadPrimitives(this);
  357. }
  358. catch (Exception e)
  359. {
  360. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: LoadPrimsFromStorage() - Failed with exception " + e.ToString());
  361. }
  362. }
  363. /// <summary>
  364. /// Loads a specific object from storage
  365. /// </summary>
  366. /// <param name="prim">The object to load</param>
  367. public void PrimFromStorage(PrimData prim)
  368. {
  369. }
  370. /// <summary>
  371. ///
  372. /// </summary>
  373. /// <param name="addPacket"></param>
  374. /// <param name="agentClient"></param>
  375. public void AddNewPrim(Packet addPacket, IClientAPI agentClient)
  376. {
  377. AddNewPrim((ObjectAddPacket)addPacket, agentClient.AgentId);
  378. }
  379. /// <summary>
  380. ///
  381. /// </summary>
  382. /// <param name="addPacket"></param>
  383. /// <param name="ownerID"></param>
  384. public void AddNewPrim(ObjectAddPacket addPacket, LLUUID ownerID)
  385. {
  386. }
  387. #endregion
  388. #region Add/Remove Avatar Methods
  389. /// <summary>
  390. ///
  391. /// </summary>
  392. /// <param name="remoteClient"></param>
  393. /// <param name="agentID"></param>
  394. /// <param name="child"></param>
  395. public override void AddNewAvatar(IClientAPI remoteClient, LLUUID agentID, bool child)
  396. {
  397. remoteClient.OnRegionHandShakeReply += new GenericCall(this.SendLayerData);
  398. //remoteClient.OnRequestWearables += new GenericCall(this.GetInitialPrims);
  399. remoteClient.OnChatFromViewer += new ChatFromViewer(this.SimChat);
  400. remoteClient.OnRequestWearables += new GenericCall(this.InformClientOfNeighbours);
  401. Avatar newAvatar = null;
  402. try
  403. {
  404. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs:AddViewerAgent() - Creating new avatar for remote viewer agent");
  405. newAvatar = new Avatar(remoteClient, this, m_clientThreads, this.m_regInfo);
  406. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs:AddViewerAgent() - Adding new avatar to world");
  407. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "World.cs:AddViewerAgent() - Starting RegionHandshake ");
  408. newAvatar.SendRegionHandshake();
  409. PhysicsVector pVec = new PhysicsVector(newAvatar.Pos.X, newAvatar.Pos.Y, newAvatar.Pos.Z);
  410. lock (this.LockPhysicsEngine)
  411. {
  412. newAvatar.PhysActor = this.phyScene.AddAvatar(pVec);
  413. }
  414. lock (Entities)
  415. {
  416. if (!Entities.ContainsKey(agentID))
  417. {
  418. this.Entities.Add(agentID, newAvatar);
  419. }
  420. else
  421. {
  422. Entities[agentID] = newAvatar;
  423. }
  424. }
  425. lock (Avatars)
  426. {
  427. if (Avatars.ContainsKey(agentID))
  428. {
  429. Avatars[agentID] = newAvatar;
  430. }
  431. else
  432. {
  433. this.Avatars.Add(agentID, newAvatar);
  434. }
  435. }
  436. }
  437. catch (Exception e)
  438. {
  439. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "World.cs: AddViewerAgent() - Failed with exception " + e.ToString());
  440. }
  441. return;
  442. }
  443. /// <summary>
  444. ///
  445. /// </summary>
  446. protected void InformClientOfNeighbours(IClientAPI remoteClient)
  447. {
  448. // Console.WriteLine("informing client of neighbouring regions");
  449. List<RegionInfo> neighbours = this.commsManager.RequestNeighbours(this.m_regInfo);
  450. //Console.WriteLine("we have " + neighbours.Count + " neighbouring regions");
  451. if (neighbours != null)
  452. {
  453. for (int i = 0; i < neighbours.Count; i++)
  454. {
  455. // Console.WriteLine("sending neighbours data");
  456. AgentCircuitData agent = remoteClient.RequestClientInfo();
  457. agent.BaseFolder = LLUUID.Zero;
  458. agent.InventoryFolder = LLUUID.Zero;
  459. agent.startpos = new LLVector3(128, 128, 70);
  460. this.commsManager.InformNeighbourOfChildAgent(neighbours[i].RegionHandle, agent);
  461. remoteClient.InformClientOfNeighbour(neighbours[i].RegionHandle, System.Net.IPAddress.Parse(neighbours[i].IPListenAddr), (ushort)neighbours[i].IPListenPort);
  462. }
  463. }
  464. }
  465. /// <summary>
  466. ///
  467. /// </summary>
  468. /// <param name="agentID"></param>
  469. public override void RemoveAvatar(LLUUID agentID)
  470. {
  471. return;
  472. }
  473. #endregion
  474. #region Request Avatars List Methods
  475. //The idea is to have a group of method that return a list of avatars meeting some requirement
  476. // ie it could be all Avatars within a certain range of the calling prim/avatar.
  477. /// <summary>
  478. ///
  479. /// </summary>
  480. /// <returns></returns>
  481. public List<Avatar> RequestAvatarList()
  482. {
  483. List<Avatar> result = new List<Avatar>();
  484. foreach (Avatar avatar in Avatars.Values)
  485. {
  486. result.Add(avatar);
  487. }
  488. return result;
  489. }
  490. #endregion
  491. #region ShutDown
  492. /// <summary>
  493. /// Tidy before shutdown
  494. /// </summary>
  495. public override void Close()
  496. {
  497. try
  498. {
  499. this.localStorage.ShutDown();
  500. }
  501. catch (Exception e)
  502. {
  503. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "World.cs: Close() - Failed with exception " + e.ToString());
  504. }
  505. }
  506. #endregion
  507. #region RegionCommsHost
  508. public void RegisterRegionWithComms()
  509. {
  510. this.regionCommsHost = this.commsManager.RegisterRegion(this.m_regInfo);
  511. if (this.regionCommsHost != null)
  512. {
  513. this.regionCommsHost.OnExpectUser += new ExpectUserDelegate(this.NewUserConnection);
  514. }
  515. }
  516. public void NewUserConnection(ulong regionHandle, AgentCircuitData agent)
  517. {
  518. Console.WriteLine("World.cs - add new user connection");
  519. //should just check that its meant for this region
  520. if (regionHandle == this.m_regInfo.RegionHandle)
  521. {
  522. this.authenticateHandler.AddNewCircuit(agent.circuitcode, agent);
  523. }
  524. }
  525. #endregion
  526. }
  527. }