TerrainModule.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 copyright
  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 OpenSim 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.Drawing;
  30. using libsecondlife;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. using OpenSim.Region.Environment.Modules.ModuleFramework;
  36. namespace OpenSim.Region.Environment.Modules.Terrain
  37. {
  38. public class TerrainModule : IRegionModule , ITerrainTemp, ICommandableModule
  39. {
  40. public enum StandardTerrainEffects : byte
  41. {
  42. Flatten = 0,
  43. Raise = 1,
  44. Lower = 2,
  45. Smooth = 3,
  46. Noise = 4,
  47. Revert = 5
  48. }
  49. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  50. private Commander m_commander = new Commander("Terrain");
  51. private Dictionary<StandardTerrainEffects, ITerrainPaintableEffect> m_painteffects =
  52. new Dictionary<StandardTerrainEffects, ITerrainPaintableEffect>();
  53. private Dictionary<StandardTerrainEffects, ITerrainFloodEffect> m_floodeffects =
  54. new Dictionary<StandardTerrainEffects, ITerrainFloodEffect>();
  55. private Dictionary<string, ITerrainLoader> m_loaders = new Dictionary<string, ITerrainLoader>();
  56. Scene m_scene;
  57. ITerrainChannel m_channel;
  58. ITerrainChannel m_revert;
  59. bool m_tainted = false;
  60. private IConfigSource m_gConfig;
  61. private void InstallDefaultEffects()
  62. {
  63. // Draggable Paint Brush Effects
  64. m_painteffects[StandardTerrainEffects.Raise] = new PaintBrushes.RaiseSphere();
  65. m_painteffects[StandardTerrainEffects.Lower] = new PaintBrushes.LowerSphere();
  66. m_painteffects[StandardTerrainEffects.Smooth] = new PaintBrushes.SmoothSphere();
  67. m_painteffects[StandardTerrainEffects.Noise] = new PaintBrushes.NoiseSphere();
  68. m_painteffects[StandardTerrainEffects.Flatten] = new PaintBrushes.FlattenSphere();
  69. m_painteffects[StandardTerrainEffects.Revert] = new PaintBrushes.RevertSphere(m_revert);
  70. // Area of effect selection effects
  71. m_floodeffects[StandardTerrainEffects.Raise] = new FloodBrushes.RaiseArea();
  72. m_floodeffects[StandardTerrainEffects.Lower] = new FloodBrushes.LowerArea();
  73. m_floodeffects[StandardTerrainEffects.Smooth] = new FloodBrushes.SmoothArea();
  74. m_floodeffects[StandardTerrainEffects.Noise] = new FloodBrushes.NoiseArea();
  75. m_floodeffects[StandardTerrainEffects.Flatten] = new FloodBrushes.FlattenArea();
  76. m_floodeffects[StandardTerrainEffects.Revert] = new FloodBrushes.RevertArea(m_revert);
  77. // Filesystem load/save loaders
  78. m_loaders[".r32"] = new FileLoaders.RAW32();
  79. m_loaders[".f32"] = m_loaders[".r32"];
  80. m_loaders[".ter"] = new FileLoaders.Terragen();
  81. m_loaders[".raw"] = new FileLoaders.LLRAW();
  82. m_loaders[".jpg"] = new FileLoaders.JPEG();
  83. m_loaders[".jpeg"] = m_loaders[".jpg"];
  84. }
  85. public void UpdateRevertMap()
  86. {
  87. int x, y;
  88. for (x = 0; x < m_channel.Width; x++)
  89. {
  90. for (y = 0; y < m_channel.Height; y++)
  91. {
  92. m_revert[x, y] = m_channel[x, y];
  93. }
  94. }
  95. }
  96. public void LoadFromFile(string filename)
  97. {
  98. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  99. {
  100. if (filename.EndsWith(loader.Key))
  101. {
  102. lock (m_scene)
  103. {
  104. try
  105. {
  106. ITerrainChannel channel = loader.Value.LoadFile(filename);
  107. m_scene.Heightmap = channel;
  108. m_channel = channel;
  109. UpdateRevertMap();
  110. }
  111. catch (NotImplementedException)
  112. {
  113. m_log.Error("[TERRAIN]: Unable to load heightmap, the " + loader.Value.ToString() + " parser does not support file loading. (May be save only)");
  114. return;
  115. }
  116. catch (System.IO.FileNotFoundException)
  117. {
  118. m_log.Error("[TERRAIN]: Unable to load heightmap, file not found. (A directory permissions error may also cause this)");
  119. return;
  120. }
  121. }
  122. m_log.Info("[TERRAIN]: File (" + filename + ") loaded successfully");
  123. return;
  124. }
  125. }
  126. m_log.Error("[TERRAIN]: Unable to load heightmap, no file loader availible for that format.");
  127. }
  128. public void LoadFromFile(string filename, int fileWidth, int fileHeight, int fileStartX, int fileStartY)
  129. {
  130. fileStartX -= (int)m_scene.RegionInfo.RegionLocX;
  131. fileStartY -= (int)m_scene.RegionInfo.RegionLocY;
  132. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  133. {
  134. if (filename.EndsWith(loader.Key))
  135. {
  136. lock (m_scene)
  137. {
  138. ITerrainChannel channel = loader.Value.LoadFile(filename, fileStartX, fileStartY,
  139. fileWidth, fileHeight, (int)Constants.RegionSize, (int)Constants.RegionSize);
  140. m_scene.Heightmap = channel;
  141. m_channel = channel;
  142. UpdateRevertMap();
  143. }
  144. return;
  145. }
  146. }
  147. }
  148. public void SaveToFile(string filename)
  149. {
  150. try
  151. {
  152. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  153. {
  154. if (filename.EndsWith(loader.Key))
  155. {
  156. loader.Value.SaveFile(filename, m_channel);
  157. return;
  158. }
  159. }
  160. }
  161. catch (NotImplementedException)
  162. {
  163. m_log.Error("Unable to save to " + filename + ", saving of this file format has not been implemented.");
  164. }
  165. }
  166. public void Initialise(Scene scene, IConfigSource config)
  167. {
  168. m_scene = scene;
  169. m_scene.RegisterModuleInterface<ITerrainTemp>(this);
  170. m_gConfig = config;
  171. // Install terrain module in the simulator
  172. if (m_scene.Heightmap == null)
  173. {
  174. lock (m_scene)
  175. {
  176. m_channel = new TerrainChannel();
  177. m_scene.Heightmap = m_channel;
  178. m_revert = new TerrainChannel();
  179. UpdateRevertMap();
  180. }
  181. }
  182. else
  183. {
  184. m_channel = m_scene.Heightmap;
  185. m_revert = new TerrainChannel();
  186. UpdateRevertMap();
  187. }
  188. m_scene.EventManager.OnNewClient += EventManager_OnNewClient;
  189. m_scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
  190. m_scene.EventManager.OnTerrainTick += EventManager_OnTerrainTick;
  191. }
  192. void EventManager_OnTerrainTick()
  193. {
  194. if (m_tainted)
  195. {
  196. m_tainted = false;
  197. m_scene.PhysicsScene.SetTerrain(m_channel.GetFloatsSerialised());
  198. m_scene.SaveTerrain();
  199. }
  200. }
  201. #region Console Commands
  202. private void InterfaceLoadFile(Object[] args)
  203. {
  204. LoadFromFile((string)args[0]);
  205. }
  206. private void InterfaceLoadTileFile(Object[] args)
  207. {
  208. LoadFromFile((string)args[0],
  209. (int)args[1],
  210. (int)args[2],
  211. (int)args[3],
  212. (int)args[4]);
  213. }
  214. private void InterfaceSaveFile(Object[] args)
  215. {
  216. SaveToFile((string)args[0]);
  217. }
  218. private void InterfaceFillTerrain(Object[] args)
  219. {
  220. int x, y;
  221. for (x = 0; x < m_channel.Width; x++)
  222. for (y = 0; y < m_channel.Height; y++)
  223. m_channel[x, y] = (double)args[0];
  224. SendUpdatedLayerData();
  225. }
  226. private void InterfaceEnableExperimentalBrushes(Object[] args)
  227. {
  228. if ((bool)args[0])
  229. {
  230. m_painteffects[StandardTerrainEffects.Revert] = new PaintBrushes.WeatherSphere();
  231. m_painteffects[StandardTerrainEffects.Flatten] = new PaintBrushes.OlsenSphere();
  232. m_painteffects[StandardTerrainEffects.Smooth] = new PaintBrushes.ErodeSphere();
  233. }
  234. else
  235. {
  236. InstallDefaultEffects();
  237. }
  238. }
  239. private void InstallInterfaces()
  240. {
  241. // Load / Save
  242. string supportedFileExtensions = "";
  243. foreach (KeyValuePair<string,ITerrainLoader> loader in m_loaders)
  244. supportedFileExtensions += " " + loader.Key + " (" + loader.Value.ToString() + ")";
  245. Command loadFromFileCommand = new Command("load", InterfaceLoadFile, "Loads a terrain from a specified file.");
  246. loadFromFileCommand.AddArgument("filename", "The file you wish to load from, the file extension determines the loader to be used. Supported extensions include: " + supportedFileExtensions, "String");
  247. Command saveToFileCommand = new Command("save", InterfaceSaveFile, "Saves the current heightmap to a specified file.");
  248. saveToFileCommand.AddArgument("filename", "The destination filename for your heightmap, the file extension determines the format to save in. Supported extensions include: " + supportedFileExtensions, "String");
  249. Command loadFromTileCommand = new Command("load-tile", InterfaceLoadTileFile, "Loads a terrain from a section of a larger file.");
  250. loadFromTileCommand.AddArgument("filename", "The file you wish to load from, the file extension determines the loader to be used. Supported extensions include: " + supportedFileExtensions, "String");
  251. loadFromTileCommand.AddArgument("file width", "The width of the file in tiles", "Integer");
  252. loadFromTileCommand.AddArgument("file height", "The height of the file in tiles", "Integer");
  253. loadFromTileCommand.AddArgument("minimum X tile", "The X region coordinate of the first section on the file", "Integer");
  254. loadFromTileCommand.AddArgument("minimum Y tile", "The Y region coordinate of the first section on the file", "Integer");
  255. // Terrain adjustments
  256. Command fillRegionCommand = new Command("fill", InterfaceFillTerrain, "Fills the current heightmap with a specified value.");
  257. fillRegionCommand.AddArgument("value", "The numeric value of the height you wish to set your region to.", "Double");
  258. // Brushes
  259. Command experimentalBrushesCommand = new Command("newbrushes", InterfaceEnableExperimentalBrushes, "Enables experimental brushes which replace the standard terrain brushes. WARNING: This is a debug setting and may be removed at any time.");
  260. experimentalBrushesCommand.AddArgument("Enabled?", "true / false - Enable new brushes", "Boolean");
  261. m_commander.RegisterCommand("load", loadFromFileCommand);
  262. m_commander.RegisterCommand("load-tile", loadFromTileCommand);
  263. m_commander.RegisterCommand("save", saveToFileCommand);
  264. m_commander.RegisterCommand("fill", fillRegionCommand);
  265. m_commander.RegisterCommand("newbrushes", experimentalBrushesCommand);
  266. // Add this to our scene so scripts can call these functions
  267. m_scene.RegisterModuleCommander("Terrain", m_commander);
  268. }
  269. #endregion
  270. void EventManager_OnPluginConsole(string[] args)
  271. {
  272. if (args[0] == "terrain")
  273. {
  274. string[] tmpArgs = new string[args.Length - 2];
  275. int i = 0;
  276. for (i = 2; i < args.Length; i++)
  277. tmpArgs[i - 2] = args[i];
  278. m_commander.ProcessConsoleCommand(args[1], tmpArgs);
  279. }
  280. }
  281. void EventManager_OnNewClient(IClientAPI client)
  282. {
  283. client.OnModifyTerrain += client_OnModifyTerrain;
  284. }
  285. void SendUpdatedLayerData()
  286. {
  287. bool shouldTaint = false;
  288. float[] serialised = m_channel.GetFloatsSerialised();
  289. int x, y;
  290. for (x = 0; x < m_channel.Width; x += Constants.TerrainPatchSize)
  291. {
  292. for (y = 0; y < m_channel.Height; y += Constants.TerrainPatchSize)
  293. {
  294. if (m_channel.Tainted(x, y))
  295. {
  296. m_scene.ForEachClient(delegate(IClientAPI controller)
  297. {
  298. controller.SendLayerData(x / Constants.TerrainPatchSize, y / Constants.TerrainPatchSize, serialised);
  299. });
  300. shouldTaint = true;
  301. }
  302. }
  303. }
  304. if (shouldTaint)
  305. {
  306. m_tainted = true;
  307. }
  308. }
  309. void client_OnModifyTerrain(float height, float seconds, byte size, byte action, float north, float west, float south, float east, IClientAPI remoteClient)
  310. {
  311. // Not a good permissions check, if in area mode, need to check the entire area.
  312. if (m_scene.PermissionsMngr.CanTerraform(remoteClient.AgentId, new LLVector3(north, west, 0)))
  313. {
  314. if (north == south && east == west)
  315. {
  316. if (m_painteffects.ContainsKey((StandardTerrainEffects)action))
  317. {
  318. m_painteffects[(StandardTerrainEffects)action].PaintEffect(
  319. m_channel, west, south, size, seconds);
  320. bool usingTerrainModule = true;
  321. if (usingTerrainModule)
  322. {
  323. SendUpdatedLayerData();
  324. }
  325. }
  326. else
  327. {
  328. m_log.Debug("Unknown terrain brush type " + action.ToString());
  329. }
  330. }
  331. else
  332. {
  333. if (m_floodeffects.ContainsKey((StandardTerrainEffects)action))
  334. {
  335. bool[,] fillArea = new bool[m_channel.Width, m_channel.Height];
  336. fillArea.Initialize();
  337. int x, y;
  338. for (x = 0; x < m_channel.Width; x++)
  339. {
  340. for (y = 0; y < m_channel.Height; y++)
  341. {
  342. if (x < east && x > west)
  343. {
  344. if (y < north && y > south)
  345. {
  346. fillArea[x, y] = true;
  347. }
  348. }
  349. }
  350. }
  351. m_floodeffects[(StandardTerrainEffects)action].FloodEffect(
  352. m_channel, fillArea, size);
  353. bool usingTerrainModule = true;
  354. if (usingTerrainModule)
  355. {
  356. SendUpdatedLayerData();
  357. }
  358. }
  359. else
  360. {
  361. m_log.Debug("Unknown terrain flood type " + action.ToString());
  362. }
  363. }
  364. }
  365. }
  366. public byte[] WriteJpegImage(string gradientmap)
  367. {
  368. byte[] imageData = null;
  369. try
  370. {
  371. Bitmap bmp = TerrainToBitmap(gradientmap);
  372. imageData = OpenJPEGNet.OpenJPEG.EncodeFromImage(bmp, true);
  373. }
  374. catch (Exception e) // LEGIT: Catching problems caused by OpenJPEG p/invoke
  375. {
  376. Console.WriteLine("Failed generating terrain map: " + e.ToString());
  377. }
  378. return imageData;
  379. }
  380. private Bitmap TerrainToBitmap(string gradientmap)
  381. {
  382. Bitmap gradientmapLd = new Bitmap(gradientmap);
  383. int pallete = gradientmapLd.Height;
  384. Bitmap bmp = new Bitmap(m_channel.Width, m_channel.Height);
  385. Color[] colours = new Color[pallete];
  386. for (int i = 0; i < pallete; i++)
  387. {
  388. colours[i] = gradientmapLd.GetPixel(0, i);
  389. }
  390. TerrainChannel copy =(TerrainChannel) m_channel.MakeCopy();
  391. for (int y = 0; y < copy.Height; y++)
  392. {
  393. for (int x = 0; x < copy.Width; x++)
  394. {
  395. // 512 is the largest possible height before colours clamp
  396. int colorindex = (int)(Math.Max(Math.Min(1.0, copy[x, y] / 512.0), 0.0) * (pallete - 1));
  397. bmp.SetPixel(x, copy.Height - y - 1, colours[colorindex]);
  398. }
  399. }
  400. return bmp;
  401. }
  402. public void PostInitialise()
  403. {
  404. InstallDefaultEffects();
  405. InstallInterfaces();
  406. }
  407. public void Close()
  408. {
  409. }
  410. public string Name
  411. {
  412. get { return "TerrainModule"; }
  413. }
  414. public bool IsSharedModule
  415. {
  416. get { return false; }
  417. }
  418. #region ICommandable Members
  419. public ICommander CommandInterface
  420. {
  421. get { return m_commander; }
  422. }
  423. #endregion
  424. }
  425. }