TerrainModule.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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 OpenSimulator 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.IO;
  30. using System.Reflection;
  31. using System.Net;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using Mono.Addins;
  36. using OpenSim.Data;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.CoreModules.Framework.InterfaceCommander;
  39. using OpenSim.Region.CoreModules.World.Terrain.FileLoaders;
  40. using OpenSim.Region.CoreModules.World.Terrain.FloodBrushes;
  41. using OpenSim.Region.CoreModules.World.Terrain.PaintBrushes;
  42. using OpenSim.Region.Framework.Interfaces;
  43. using OpenSim.Region.Framework.Scenes;
  44. namespace OpenSim.Region.CoreModules.World.Terrain
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "TerrainModule")]
  47. public class TerrainModule : INonSharedRegionModule, ICommandableModule, ITerrainModule
  48. {
  49. #region StandardTerrainEffects enum
  50. /// <summary>
  51. /// A standard set of terrain brushes and effects recognised by viewers
  52. /// </summary>
  53. public enum StandardTerrainEffects : byte
  54. {
  55. Flatten = 0,
  56. Raise = 1,
  57. Lower = 2,
  58. Smooth = 3,
  59. Noise = 4,
  60. Revert = 5,
  61. // Extended brushes
  62. Erode = 255,
  63. Weather = 254,
  64. Olsen = 253
  65. }
  66. #endregion
  67. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  68. private static readonly string LogHeader = "[TERRAIN MODULE]";
  69. private readonly Commander m_commander = new Commander("terrain");
  70. private readonly Dictionary<StandardTerrainEffects, ITerrainFloodEffect> m_floodeffects =
  71. new Dictionary<StandardTerrainEffects, ITerrainFloodEffect>();
  72. private readonly Dictionary<string, ITerrainLoader> m_loaders = new Dictionary<string, ITerrainLoader>();
  73. private readonly Dictionary<StandardTerrainEffects, ITerrainPaintableEffect> m_painteffects =
  74. new Dictionary<StandardTerrainEffects, ITerrainPaintableEffect>();
  75. private ITerrainChannel m_channel;
  76. private Dictionary<string, ITerrainEffect> m_plugineffects;
  77. private ITerrainChannel m_revert;
  78. private Scene m_scene;
  79. private volatile bool m_tainted;
  80. private readonly Stack<LandUndoState> m_undo = new Stack<LandUndoState>(5);
  81. private String m_InitialTerrain = "pinhead-island";
  82. /// <summary>
  83. /// Human readable list of terrain file extensions that are supported.
  84. /// </summary>
  85. private string m_supportedFileExtensions = "";
  86. //For terrain save-tile file extensions
  87. private string m_supportFileExtensionsForTileSave = "";
  88. #region ICommandableModule Members
  89. public ICommander CommandInterface
  90. {
  91. get { return m_commander; }
  92. }
  93. #endregion
  94. #region INonSharedRegionModule Members
  95. /// <summary>
  96. /// Creates and initialises a terrain module for a region
  97. /// </summary>
  98. /// <param name="scene">Region initialising</param>
  99. /// <param name="config">Config for the region</param>
  100. public void Initialise(IConfigSource config)
  101. {
  102. IConfig terrainConfig = config.Configs["Terrain"];
  103. if (terrainConfig != null)
  104. m_InitialTerrain = terrainConfig.GetString("InitialTerrain", m_InitialTerrain);
  105. }
  106. public void AddRegion(Scene scene)
  107. {
  108. m_scene = scene;
  109. // Install terrain module in the simulator
  110. lock (m_scene)
  111. {
  112. if (m_scene.Heightmap == null)
  113. {
  114. m_channel = new TerrainChannel(m_InitialTerrain, (int)m_scene.RegionInfo.RegionSizeX,
  115. (int)m_scene.RegionInfo.RegionSizeY,
  116. (int)m_scene.RegionInfo.RegionSizeZ);
  117. m_scene.Heightmap = m_channel;
  118. UpdateRevertMap();
  119. }
  120. else
  121. {
  122. m_channel = m_scene.Heightmap;
  123. UpdateRevertMap();
  124. }
  125. m_scene.RegisterModuleInterface<ITerrainModule>(this);
  126. m_scene.EventManager.OnNewClient += EventManager_OnNewClient;
  127. m_scene.EventManager.OnClientClosed += EventManager_OnClientClosed;
  128. m_scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
  129. m_scene.EventManager.OnTerrainTick += EventManager_OnTerrainTick;
  130. m_scene.EventManager.OnFrame += EventManager_OnFrame;
  131. }
  132. InstallDefaultEffects();
  133. LoadPlugins();
  134. // Generate user-readable extensions list
  135. string supportedFilesSeparator = "";
  136. string supportedFilesSeparatorForTileSave = "";
  137. m_supportFileExtensionsForTileSave = "";
  138. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  139. {
  140. m_supportedFileExtensions += supportedFilesSeparator + loader.Key + " (" + loader.Value + ")";
  141. supportedFilesSeparator = ", ";
  142. //For terrain save-tile file extensions
  143. if (loader.Value.SupportsTileSave() == true)
  144. {
  145. m_supportFileExtensionsForTileSave += supportedFilesSeparatorForTileSave + loader.Key + " (" + loader.Value + ")";
  146. supportedFilesSeparatorForTileSave = ", ";
  147. }
  148. }
  149. }
  150. public void RegionLoaded(Scene scene)
  151. {
  152. //Do this here to give file loaders time to initialize and
  153. //register their supported file extensions and file formats.
  154. InstallInterfaces();
  155. }
  156. public void RemoveRegion(Scene scene)
  157. {
  158. lock (m_scene)
  159. {
  160. // remove the commands
  161. m_scene.UnregisterModuleCommander(m_commander.Name);
  162. // remove the event-handlers
  163. m_scene.EventManager.OnFrame -= EventManager_OnFrame;
  164. m_scene.EventManager.OnTerrainTick -= EventManager_OnTerrainTick;
  165. m_scene.EventManager.OnPluginConsole -= EventManager_OnPluginConsole;
  166. m_scene.EventManager.OnClientClosed -= EventManager_OnClientClosed;
  167. m_scene.EventManager.OnNewClient -= EventManager_OnNewClient;
  168. // remove the interface
  169. m_scene.UnregisterModuleInterface<ITerrainModule>(this);
  170. }
  171. }
  172. public void Close()
  173. {
  174. }
  175. public Type ReplaceableInterface
  176. {
  177. get { return null; }
  178. }
  179. public string Name
  180. {
  181. get { return "TerrainModule"; }
  182. }
  183. #endregion
  184. #region ITerrainModule Members
  185. public void UndoTerrain(ITerrainChannel channel)
  186. {
  187. m_channel = channel;
  188. }
  189. /// <summary>
  190. /// Loads a terrain file from disk and installs it in the scene.
  191. /// </summary>
  192. /// <param name="filename">Filename to terrain file. Type is determined by extension.</param>
  193. public void LoadFromFile(string filename)
  194. {
  195. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  196. {
  197. if (filename.EndsWith(loader.Key))
  198. {
  199. lock (m_scene)
  200. {
  201. try
  202. {
  203. ITerrainChannel channel = loader.Value.LoadFile(filename);
  204. if (channel.Width != m_scene.RegionInfo.RegionSizeX || channel.Height != m_scene.RegionInfo.RegionSizeY)
  205. {
  206. // TerrainChannel expects a RegionSize x RegionSize map, currently
  207. throw new ArgumentException(String.Format("wrong size, use a file with size {0} x {1}",
  208. m_scene.RegionInfo.RegionSizeX, m_scene.RegionInfo.RegionSizeY));
  209. }
  210. m_log.DebugFormat("[TERRAIN]: Loaded terrain, wd/ht: {0}/{1}", channel.Width, channel.Height);
  211. m_scene.Heightmap = channel;
  212. m_channel = channel;
  213. UpdateRevertMap();
  214. }
  215. catch (NotImplementedException)
  216. {
  217. m_log.Error("[TERRAIN]: Unable to load heightmap, the " + loader.Value +
  218. " parser does not support file loading. (May be save only)");
  219. throw new TerrainException(String.Format("unable to load heightmap: parser {0} does not support loading", loader.Value));
  220. }
  221. catch (FileNotFoundException)
  222. {
  223. m_log.Error(
  224. "[TERRAIN]: Unable to load heightmap, file not found. (A directory permissions error may also cause this)");
  225. throw new TerrainException(
  226. String.Format("unable to load heightmap: file {0} not found (or permissions do not allow access", filename));
  227. }
  228. catch (ArgumentException e)
  229. {
  230. m_log.ErrorFormat("[TERRAIN]: Unable to load heightmap: {0}", e.Message);
  231. throw new TerrainException(
  232. String.Format("Unable to load heightmap: {0}", e.Message));
  233. }
  234. }
  235. m_log.Info("[TERRAIN]: File (" + filename + ") loaded successfully");
  236. return;
  237. }
  238. }
  239. m_log.Error("[TERRAIN]: Unable to load heightmap, no file loader available for that format.");
  240. throw new TerrainException(String.Format("unable to load heightmap from file {0}: no loader available for that format", filename));
  241. }
  242. /// <summary>
  243. /// Saves the current heightmap to a specified file.
  244. /// </summary>
  245. /// <param name="filename">The destination filename</param>
  246. public void SaveToFile(string filename)
  247. {
  248. try
  249. {
  250. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  251. {
  252. if (filename.EndsWith(loader.Key))
  253. {
  254. loader.Value.SaveFile(filename, m_channel);
  255. m_log.InfoFormat("[TERRAIN]: Saved terrain from {0} to {1}", m_scene.RegionInfo.RegionName, filename);
  256. return;
  257. }
  258. }
  259. }
  260. catch (IOException ioe)
  261. {
  262. m_log.Error(String.Format("[TERRAIN]: Unable to save to {0}, {1}", filename, ioe.Message));
  263. }
  264. m_log.ErrorFormat(
  265. "[TERRAIN]: Could not save terrain from {0} to {1}. Valid file extensions are {2}",
  266. m_scene.RegionInfo.RegionName, filename, m_supportedFileExtensions);
  267. }
  268. /// <summary>
  269. /// Loads a terrain file from the specified URI
  270. /// </summary>
  271. /// <param name="filename">The name of the terrain to load</param>
  272. /// <param name="pathToTerrainHeightmap">The URI to the terrain height map</param>
  273. public void LoadFromStream(string filename, Uri pathToTerrainHeightmap)
  274. {
  275. LoadFromStream(filename, URIFetch(pathToTerrainHeightmap));
  276. }
  277. public void LoadFromStream(string filename, Stream stream)
  278. {
  279. LoadFromStream(filename, Vector3.Zero, 0f, Vector2.Zero, stream);
  280. }
  281. /// <summary>
  282. /// Loads a terrain file from a stream and installs it in the scene.
  283. /// </summary>
  284. /// <param name="filename">Filename to terrain file. Type is determined by extension.</param>
  285. /// <param name="stream"></param>
  286. public void LoadFromStream(string filename, Vector3 displacement,
  287. float radianRotation, Vector2 rotationDisplacement, Stream stream)
  288. {
  289. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  290. {
  291. if (filename.EndsWith(loader.Key))
  292. {
  293. lock (m_scene)
  294. {
  295. try
  296. {
  297. ITerrainChannel channel = loader.Value.LoadStream(stream);
  298. m_channel.Merge(channel, displacement, radianRotation, rotationDisplacement);
  299. UpdateRevertMap();
  300. }
  301. catch (NotImplementedException)
  302. {
  303. m_log.Error("[TERRAIN]: Unable to load heightmap, the " + loader.Value +
  304. " parser does not support file loading. (May be save only)");
  305. throw new TerrainException(String.Format("unable to load heightmap: parser {0} does not support loading", loader.Value));
  306. }
  307. }
  308. m_log.Info("[TERRAIN]: File (" + filename + ") loaded successfully");
  309. return;
  310. }
  311. }
  312. m_log.Error("[TERRAIN]: Unable to load heightmap, no file loader available for that format.");
  313. throw new TerrainException(String.Format("unable to load heightmap from file {0}: no loader available for that format", filename));
  314. }
  315. private static Stream URIFetch(Uri uri)
  316. {
  317. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
  318. // request.Credentials = credentials;
  319. request.ContentLength = 0;
  320. request.KeepAlive = false;
  321. WebResponse response = request.GetResponse();
  322. Stream file = response.GetResponseStream();
  323. if (response.ContentLength == 0)
  324. throw new Exception(String.Format("{0} returned an empty file", uri.ToString()));
  325. // return new BufferedStream(file, (int) response.ContentLength);
  326. return new BufferedStream(file, 1000000);
  327. }
  328. /// <summary>
  329. /// Modify Land
  330. /// </summary>
  331. /// <param name="pos">Land-position (X,Y,0)</param>
  332. /// <param name="size">The size of the brush (0=small, 1=medium, 2=large)</param>
  333. /// <param name="action">0=LAND_LEVEL, 1=LAND_RAISE, 2=LAND_LOWER, 3=LAND_SMOOTH, 4=LAND_NOISE, 5=LAND_REVERT</param>
  334. /// <param name="agentId">UUID of script-owner</param>
  335. public void ModifyTerrain(UUID user, Vector3 pos, byte size, byte action, UUID agentId)
  336. {
  337. float duration = 0.25f;
  338. if (action == 0)
  339. duration = 4.0f;
  340. client_OnModifyTerrain(user, (float)pos.Z, duration, size, action, pos.Y, pos.X, pos.Y, pos.X, agentId);
  341. }
  342. /// <summary>
  343. /// Saves the current heightmap to a specified stream.
  344. /// </summary>
  345. /// <param name="filename">The destination filename. Used here only to identify the image type</param>
  346. /// <param name="stream"></param>
  347. public void SaveToStream(string filename, Stream stream)
  348. {
  349. try
  350. {
  351. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  352. {
  353. if (filename.EndsWith(loader.Key))
  354. {
  355. loader.Value.SaveStream(stream, m_channel);
  356. return;
  357. }
  358. }
  359. }
  360. catch (NotImplementedException)
  361. {
  362. m_log.Error("Unable to save to " + filename + ", saving of this file format has not been implemented.");
  363. throw new TerrainException(String.Format("Unable to save heightmap: saving of this file format not implemented"));
  364. }
  365. }
  366. public void TaintTerrain ()
  367. {
  368. m_channel.GetTerrainData().TaintAllTerrain();
  369. }
  370. #region Plugin Loading Methods
  371. private void LoadPlugins()
  372. {
  373. m_plugineffects = new Dictionary<string, ITerrainEffect>();
  374. LoadPlugins(Assembly.GetCallingAssembly());
  375. string plugineffectsPath = "Terrain";
  376. // Load the files in the Terrain/ dir
  377. if (!Directory.Exists(plugineffectsPath))
  378. return;
  379. string[] files = Directory.GetFiles(plugineffectsPath);
  380. foreach (string file in files)
  381. {
  382. m_log.Info("Loading effects in " + file);
  383. try
  384. {
  385. Assembly library = Assembly.LoadFrom(file);
  386. LoadPlugins(library);
  387. }
  388. catch (BadImageFormatException)
  389. {
  390. }
  391. }
  392. }
  393. private void LoadPlugins(Assembly library)
  394. {
  395. foreach (Type pluginType in library.GetTypes())
  396. {
  397. try
  398. {
  399. if (pluginType.IsAbstract || pluginType.IsNotPublic)
  400. continue;
  401. string typeName = pluginType.Name;
  402. if (pluginType.GetInterface("ITerrainEffect", false) != null)
  403. {
  404. ITerrainEffect terEffect = (ITerrainEffect)Activator.CreateInstance(library.GetType(pluginType.ToString()));
  405. InstallPlugin(typeName, terEffect);
  406. }
  407. else if (pluginType.GetInterface("ITerrainLoader", false) != null)
  408. {
  409. ITerrainLoader terLoader = (ITerrainLoader)Activator.CreateInstance(library.GetType(pluginType.ToString()));
  410. m_loaders[terLoader.FileExtension] = terLoader;
  411. m_log.Info("L ... " + typeName);
  412. }
  413. }
  414. catch (AmbiguousMatchException)
  415. {
  416. }
  417. }
  418. }
  419. public void InstallPlugin(string pluginName, ITerrainEffect effect)
  420. {
  421. lock (m_plugineffects)
  422. {
  423. if (!m_plugineffects.ContainsKey(pluginName))
  424. {
  425. m_plugineffects.Add(pluginName, effect);
  426. m_log.Info("E ... " + pluginName);
  427. }
  428. else
  429. {
  430. m_plugineffects[pluginName] = effect;
  431. m_log.Info("E ... " + pluginName + " (Replaced)");
  432. }
  433. }
  434. }
  435. #endregion
  436. #endregion
  437. /// <summary>
  438. /// Installs into terrain module the standard suite of brushes
  439. /// </summary>
  440. private void InstallDefaultEffects()
  441. {
  442. // Draggable Paint Brush Effects
  443. m_painteffects[StandardTerrainEffects.Raise] = new RaiseSphere();
  444. m_painteffects[StandardTerrainEffects.Lower] = new LowerSphere();
  445. m_painteffects[StandardTerrainEffects.Smooth] = new SmoothSphere();
  446. m_painteffects[StandardTerrainEffects.Noise] = new NoiseSphere();
  447. m_painteffects[StandardTerrainEffects.Flatten] = new FlattenSphere();
  448. m_painteffects[StandardTerrainEffects.Revert] = new RevertSphere(m_revert);
  449. m_painteffects[StandardTerrainEffects.Erode] = new ErodeSphere();
  450. m_painteffects[StandardTerrainEffects.Weather] = new WeatherSphere();
  451. m_painteffects[StandardTerrainEffects.Olsen] = new OlsenSphere();
  452. // Area of effect selection effects
  453. m_floodeffects[StandardTerrainEffects.Raise] = new RaiseArea();
  454. m_floodeffects[StandardTerrainEffects.Lower] = new LowerArea();
  455. m_floodeffects[StandardTerrainEffects.Smooth] = new SmoothArea();
  456. m_floodeffects[StandardTerrainEffects.Noise] = new NoiseArea();
  457. m_floodeffects[StandardTerrainEffects.Flatten] = new FlattenArea();
  458. m_floodeffects[StandardTerrainEffects.Revert] = new RevertArea(m_revert);
  459. // Filesystem load/save loaders
  460. m_loaders[".r32"] = new RAW32();
  461. m_loaders[".f32"] = m_loaders[".r32"];
  462. m_loaders[".ter"] = new Terragen();
  463. m_loaders[".raw"] = new LLRAW();
  464. m_loaders[".jpg"] = new JPEG();
  465. m_loaders[".jpeg"] = m_loaders[".jpg"];
  466. m_loaders[".bmp"] = new BMP();
  467. m_loaders[".png"] = new PNG();
  468. m_loaders[".gif"] = new GIF();
  469. m_loaders[".tif"] = new TIFF();
  470. m_loaders[".tiff"] = m_loaders[".tif"];
  471. }
  472. /// <summary>
  473. /// Saves the current state of the region into the revert map buffer.
  474. /// </summary>
  475. public void UpdateRevertMap()
  476. {
  477. /*
  478. int x;
  479. for (x = 0; x < m_channel.Width; x++)
  480. {
  481. int y;
  482. for (y = 0; y < m_channel.Height; y++)
  483. {
  484. m_revert[x, y] = m_channel[x, y];
  485. }
  486. }
  487. */
  488. m_revert = m_channel.MakeCopy();
  489. }
  490. /// <summary>
  491. /// Loads a tile from a larger terrain file and installs it into the region.
  492. /// </summary>
  493. /// <param name="filename">The terrain file to load</param>
  494. /// <param name="fileWidth">The width of the file in units</param>
  495. /// <param name="fileHeight">The height of the file in units</param>
  496. /// <param name="fileStartX">Where to begin our slice</param>
  497. /// <param name="fileStartY">Where to begin our slice</param>
  498. public void LoadFromFile(string filename, int fileWidth, int fileHeight, int fileStartX, int fileStartY)
  499. {
  500. int offsetX = (int) m_scene.RegionInfo.RegionLocX - fileStartX;
  501. int offsetY = (int) m_scene.RegionInfo.RegionLocY - fileStartY;
  502. if (offsetX >= 0 && offsetX < fileWidth && offsetY >= 0 && offsetY < fileHeight)
  503. {
  504. // this region is included in the tile request
  505. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  506. {
  507. if (filename.EndsWith(loader.Key))
  508. {
  509. lock (m_scene)
  510. {
  511. ITerrainChannel channel = loader.Value.LoadFile(filename, offsetX, offsetY,
  512. fileWidth, fileHeight,
  513. (int) m_scene.RegionInfo.RegionSizeX,
  514. (int) m_scene.RegionInfo.RegionSizeY);
  515. m_scene.Heightmap = channel;
  516. m_channel = channel;
  517. UpdateRevertMap();
  518. }
  519. return;
  520. }
  521. }
  522. }
  523. }
  524. /// <summary>
  525. /// Save a number of map tiles to a single big image file.
  526. /// </summary>
  527. /// <remarks>
  528. /// If the image file already exists then the tiles saved will replace those already in the file - other tiles
  529. /// will be untouched.
  530. /// </remarks>
  531. /// <param name="filename">The terrain file to save</param>
  532. /// <param name="fileWidth">The number of tiles to save along the X axis.</param>
  533. /// <param name="fileHeight">The number of tiles to save along the Y axis.</param>
  534. /// <param name="fileStartX">The map x co-ordinate at which to begin the save.</param>
  535. /// <param name="fileStartY">The may y co-ordinate at which to begin the save.</param>
  536. public void SaveToFile(string filename, int fileWidth, int fileHeight, int fileStartX, int fileStartY)
  537. {
  538. int offsetX = (int)m_scene.RegionInfo.RegionLocX - fileStartX;
  539. int offsetY = (int)m_scene.RegionInfo.RegionLocY - fileStartY;
  540. if (offsetX < 0 || offsetX >= fileWidth || offsetY < 0 || offsetY >= fileHeight)
  541. {
  542. MainConsole.Instance.OutputFormat(
  543. "ERROR: file width + minimum X tile and file height + minimum Y tile must incorporate the current region at ({0},{1}). File width {2} from {3} and file height {4} from {5} does not.",
  544. m_scene.RegionInfo.RegionLocX, m_scene.RegionInfo.RegionLocY, fileWidth, fileStartX, fileHeight, fileStartY);
  545. return;
  546. }
  547. // this region is included in the tile request
  548. foreach (KeyValuePair<string, ITerrainLoader> loader in m_loaders)
  549. {
  550. if (filename.EndsWith(loader.Key) && loader.Value.SupportsTileSave())
  551. {
  552. lock (m_scene)
  553. {
  554. loader.Value.SaveFile(m_channel, filename, offsetX, offsetY,
  555. fileWidth, fileHeight,
  556. (int)m_scene.RegionInfo.RegionSizeX,
  557. (int)m_scene.RegionInfo.RegionSizeY);
  558. MainConsole.Instance.OutputFormat(
  559. "Saved terrain from ({0},{1}) to ({2},{3}) from {4} to {5}",
  560. fileStartX, fileStartY, fileStartX + fileWidth - 1, fileStartY + fileHeight - 1,
  561. m_scene.RegionInfo.RegionName, filename);
  562. }
  563. return;
  564. }
  565. }
  566. MainConsole.Instance.OutputFormat(
  567. "ERROR: Could not save terrain from {0} to {1}. Valid file extensions are {2}",
  568. m_scene.RegionInfo.RegionName, filename, m_supportFileExtensionsForTileSave);
  569. }
  570. /// <summary>
  571. /// Called before processing of every simulation frame.
  572. /// This is used to check to see of any of the terrain is tainted and, if so, schedule
  573. /// updates for all the presences.
  574. /// This also checks to see if there are updates that need to be sent for each presence.
  575. /// This is where the logic is to send terrain updates to clients.
  576. /// </summary>
  577. private void EventManager_OnFrame()
  578. {
  579. TerrainData terrData = m_channel.GetTerrainData();
  580. bool shouldTaint = false;
  581. for (int x = 0; x < terrData.SizeX; x += Constants.TerrainPatchSize)
  582. {
  583. for (int y = 0; y < terrData.SizeY; y += Constants.TerrainPatchSize)
  584. {
  585. if (terrData.IsTaintedAt(x, y))
  586. {
  587. // Found a patch that was modified. Push this flag into the clients.
  588. SendToClients(terrData, x, y);
  589. shouldTaint = true;
  590. }
  591. }
  592. }
  593. if (shouldTaint)
  594. {
  595. m_scene.EventManager.TriggerTerrainTainted();
  596. m_tainted = true;
  597. }
  598. }
  599. /// <summary>
  600. /// Performs updates to the region periodically, synchronising physics and other heightmap aware sections
  601. /// Called infrequently (like every 5 seconds or so). Best used for storing terrain.
  602. /// </summary>
  603. private void EventManager_OnTerrainTick()
  604. {
  605. if (m_tainted)
  606. {
  607. m_tainted = false;
  608. m_scene.PhysicsScene.SetTerrain(m_channel.GetFloatsSerialised());
  609. m_scene.SaveTerrain();
  610. // Clients who look at the map will never see changes after they looked at the map, so i've commented this out.
  611. //m_scene.CreateTerrainTexture(true);
  612. }
  613. }
  614. /// <summary>
  615. /// Processes commandline input. Do not call directly.
  616. /// </summary>
  617. /// <param name="args">Commandline arguments</param>
  618. private void EventManager_OnPluginConsole(string[] args)
  619. {
  620. if (args[0] == "terrain")
  621. {
  622. if (args.Length == 1)
  623. {
  624. m_commander.ProcessConsoleCommand("help", new string[0]);
  625. return;
  626. }
  627. string[] tmpArgs = new string[args.Length - 2];
  628. int i;
  629. for (i = 2; i < args.Length; i++)
  630. tmpArgs[i - 2] = args[i];
  631. m_commander.ProcessConsoleCommand(args[1], tmpArgs);
  632. }
  633. }
  634. /// <summary>
  635. /// Installs terrain brush hook to IClientAPI
  636. /// </summary>
  637. /// <param name="client"></param>
  638. private void EventManager_OnNewClient(IClientAPI client)
  639. {
  640. client.OnModifyTerrain += client_OnModifyTerrain;
  641. client.OnBakeTerrain += client_OnBakeTerrain;
  642. client.OnLandUndo += client_OnLandUndo;
  643. client.OnUnackedTerrain += client_OnUnackedTerrain;
  644. }
  645. /// <summary>
  646. /// Installs terrain brush hook to IClientAPI
  647. /// </summary>
  648. /// <param name="client"></param>
  649. private void EventManager_OnClientClosed(UUID client, Scene scene)
  650. {
  651. ScenePresence presence = scene.GetScenePresence(client);
  652. if (presence != null)
  653. {
  654. presence.ControllingClient.OnModifyTerrain -= client_OnModifyTerrain;
  655. presence.ControllingClient.OnBakeTerrain -= client_OnBakeTerrain;
  656. presence.ControllingClient.OnLandUndo -= client_OnLandUndo;
  657. presence.ControllingClient.OnUnackedTerrain -= client_OnUnackedTerrain;
  658. }
  659. }
  660. /// <summary>
  661. /// Checks to see if the terrain has been modified since last check
  662. /// but won't attempt to limit those changes to the limits specified in the estate settings
  663. /// currently invoked by the command line operations in the region server only
  664. /// </summary>
  665. private void CheckForTerrainUpdates()
  666. {
  667. CheckForTerrainUpdates(false);
  668. }
  669. /// <summary>
  670. /// Checks to see if the terrain has been modified since last check.
  671. /// If it has been modified, every all the terrain patches are sent to the client.
  672. /// If the call is asked to respect the estate settings for terrain_raise_limit and
  673. /// terrain_lower_limit, it will clamp terrain updates between these values
  674. /// currently invoked by client_OnModifyTerrain only and not the Commander interfaces
  675. /// <param name="respectEstateSettings">should height map deltas be limited to the estate settings limits</param>
  676. /// </summary>
  677. private void CheckForTerrainUpdates(bool respectEstateSettings)
  678. {
  679. }
  680. /// <summary>
  681. /// Scan over changes in the terrain and limit height changes. This enforces the
  682. /// non-estate owner limits on rate of terrain editting.
  683. /// Returns 'true' if any heights were limited.
  684. /// </summary>
  685. private bool EnforceEstateLimits()
  686. {
  687. TerrainData terrData = m_channel.GetTerrainData();
  688. bool wasLimited = false;
  689. for (int x = 0; x < terrData.SizeX; x += Constants.TerrainPatchSize)
  690. {
  691. for (int y = 0; y < terrData.SizeY; y += Constants.TerrainPatchSize)
  692. {
  693. if (terrData.IsTaintedAt(x, y, false /* clearOnTest */))
  694. {
  695. // If we should respect the estate settings then
  696. // fixup and height deltas that don't respect them.
  697. // Note that LimitChannelChanges() modifies the TerrainChannel with the limited height values.
  698. wasLimited |= LimitChannelChanges(terrData, x, y);
  699. }
  700. }
  701. }
  702. return wasLimited;
  703. }
  704. /// <summary>
  705. /// Checks to see height deltas in the tainted terrain patch at xStart ,yStart
  706. /// are all within the current estate limits
  707. /// <returns>true if changes were limited, false otherwise</returns>
  708. /// </summary>
  709. private bool LimitChannelChanges(TerrainData terrData, int xStart, int yStart)
  710. {
  711. bool changesLimited = false;
  712. float minDelta = (float)m_scene.RegionInfo.RegionSettings.TerrainLowerLimit;
  713. float maxDelta = (float)m_scene.RegionInfo.RegionSettings.TerrainRaiseLimit;
  714. // loop through the height map for this patch and compare it against
  715. // the revert map
  716. for (int x = xStart; x < xStart + Constants.TerrainPatchSize; x++)
  717. {
  718. for (int y = yStart; y < yStart + Constants.TerrainPatchSize; y++)
  719. {
  720. float requestedHeight = terrData[x, y];
  721. float bakedHeight = (float)m_revert[x, y];
  722. float requestedDelta = requestedHeight - bakedHeight;
  723. if (requestedDelta > maxDelta)
  724. {
  725. terrData[x, y] = bakedHeight + maxDelta;
  726. changesLimited = true;
  727. }
  728. else if (requestedDelta < minDelta)
  729. {
  730. terrData[x, y] = bakedHeight + minDelta; //as lower is a -ve delta
  731. changesLimited = true;
  732. }
  733. }
  734. }
  735. return changesLimited;
  736. }
  737. private void client_OnLandUndo(IClientAPI client)
  738. {
  739. lock (m_undo)
  740. {
  741. if (m_undo.Count > 0)
  742. {
  743. LandUndoState goback = m_undo.Pop();
  744. if (goback != null)
  745. goback.PlaybackState();
  746. }
  747. }
  748. }
  749. /// <summary>
  750. /// Sends a copy of the current terrain to the scenes clients
  751. /// </summary>
  752. /// <param name="serialised">A copy of the terrain as a 1D float array of size w*h</param>
  753. /// <param name="x">The patch corner to send</param>
  754. /// <param name="y">The patch corner to send</param>
  755. private void SendToClients(TerrainData terrData, int x, int y)
  756. {
  757. // We know the actual terrain data passed is ignored. This kludge saves changing IClientAPI.
  758. //float[] heightMap = terrData.GetFloatsSerialized();
  759. float[] heightMap = new float[10];
  760. m_scene.ForEachClient(
  761. delegate(IClientAPI controller)
  762. {
  763. controller.SendLayerData( x / Constants.TerrainPatchSize,
  764. y / Constants.TerrainPatchSize,
  765. heightMap);
  766. }
  767. );
  768. }
  769. private void client_OnModifyTerrain(UUID user, float height, float seconds, byte size, byte action,
  770. float north, float west, float south, float east, UUID agentId)
  771. {
  772. bool god = m_scene.Permissions.IsGod(user);
  773. bool allowed = false;
  774. if (north == south && east == west)
  775. {
  776. if (m_painteffects.ContainsKey((StandardTerrainEffects) action))
  777. {
  778. bool[,] allowMask = new bool[m_channel.Width,m_channel.Height];
  779. allowMask.Initialize();
  780. int n = size + 1;
  781. if (n > 2)
  782. n = 4;
  783. int zx = (int) (west + 0.5);
  784. int zy = (int) (north + 0.5);
  785. int dx;
  786. for (dx=-n; dx<=n; dx++)
  787. {
  788. int dy;
  789. for (dy=-n; dy<=n; dy++)
  790. {
  791. int x = zx + dx;
  792. int y = zy + dy;
  793. if (x>=0 && y>=0 && x<m_channel.Width && y<m_channel.Height)
  794. {
  795. if (m_scene.Permissions.CanTerraformLand(agentId, new Vector3(x,y,0)))
  796. {
  797. allowMask[x, y] = true;
  798. allowed = true;
  799. }
  800. }
  801. }
  802. }
  803. if (allowed)
  804. {
  805. StoreUndoState();
  806. m_painteffects[(StandardTerrainEffects) action].PaintEffect(
  807. m_channel, allowMask, west, south, height, size, seconds);
  808. //revert changes outside estate limits
  809. if (!god)
  810. EnforceEstateLimits();
  811. }
  812. }
  813. else
  814. {
  815. m_log.Debug("Unknown terrain brush type " + action);
  816. }
  817. }
  818. else
  819. {
  820. if (m_floodeffects.ContainsKey((StandardTerrainEffects) action))
  821. {
  822. bool[,] fillArea = new bool[m_channel.Width,m_channel.Height];
  823. fillArea.Initialize();
  824. int x;
  825. for (x = 0; x < m_channel.Width; x++)
  826. {
  827. int y;
  828. for (y = 0; y < m_channel.Height; y++)
  829. {
  830. if (x < east && x > west)
  831. {
  832. if (y < north && y > south)
  833. {
  834. if (m_scene.Permissions.CanTerraformLand(agentId, new Vector3(x,y,0)))
  835. {
  836. fillArea[x, y] = true;
  837. allowed = true;
  838. }
  839. }
  840. }
  841. }
  842. }
  843. if (allowed)
  844. {
  845. StoreUndoState();
  846. m_floodeffects[(StandardTerrainEffects) action].FloodEffect(
  847. m_channel, fillArea, size);
  848. //revert changes outside estate limits
  849. if (!god)
  850. EnforceEstateLimits();
  851. }
  852. }
  853. else
  854. {
  855. m_log.Debug("Unknown terrain flood type " + action);
  856. }
  857. }
  858. }
  859. private void client_OnBakeTerrain(IClientAPI remoteClient)
  860. {
  861. // Not a good permissions check (see client_OnModifyTerrain above), need to check the entire area.
  862. // for now check a point in the centre of the region
  863. if (m_scene.Permissions.CanIssueEstateCommand(remoteClient.AgentId, true))
  864. {
  865. InterfaceBakeTerrain(null); //bake terrain does not use the passed in parameter
  866. }
  867. }
  868. protected void client_OnUnackedTerrain(IClientAPI client, int patchX, int patchY)
  869. {
  870. //m_log.Debug("Terrain packet unacked, resending patch: " + patchX + " , " + patchY);
  871. // SendLayerData does not use the heightmap parameter. This kludge is so as to not change IClientAPI.
  872. float[] heightMap = new float[10];
  873. client.SendLayerData(patchX, patchY, heightMap);
  874. }
  875. private void StoreUndoState()
  876. {
  877. lock (m_undo)
  878. {
  879. if (m_undo.Count > 0)
  880. {
  881. LandUndoState last = m_undo.Peek();
  882. if (last != null)
  883. {
  884. if (last.Compare(m_channel))
  885. return;
  886. }
  887. }
  888. LandUndoState nUndo = new LandUndoState(this, m_channel);
  889. m_undo.Push(nUndo);
  890. }
  891. }
  892. #region Console Commands
  893. private void InterfaceLoadFile(Object[] args)
  894. {
  895. LoadFromFile((string) args[0]);
  896. }
  897. private void InterfaceLoadTileFile(Object[] args)
  898. {
  899. LoadFromFile((string) args[0],
  900. (int) args[1],
  901. (int) args[2],
  902. (int) args[3],
  903. (int) args[4]);
  904. }
  905. private void InterfaceSaveFile(Object[] args)
  906. {
  907. SaveToFile((string) args[0]);
  908. }
  909. private void InterfaceSaveTileFile(Object[] args)
  910. {
  911. SaveToFile((string)args[0],
  912. (int)args[1],
  913. (int)args[2],
  914. (int)args[3],
  915. (int)args[4]);
  916. }
  917. private void InterfaceBakeTerrain(Object[] args)
  918. {
  919. UpdateRevertMap();
  920. }
  921. private void InterfaceRevertTerrain(Object[] args)
  922. {
  923. int x, y;
  924. for (x = 0; x < m_channel.Width; x++)
  925. for (y = 0; y < m_channel.Height; y++)
  926. m_channel[x, y] = m_revert[x, y];
  927. }
  928. private void InterfaceFlipTerrain(Object[] args)
  929. {
  930. String direction = (String)args[0];
  931. if (direction.ToLower().StartsWith("y"))
  932. {
  933. for (int x = 0; x < m_channel.Width; x++)
  934. {
  935. for (int y = 0; y < m_channel.Height / 2; y++)
  936. {
  937. double height = m_channel[x, y];
  938. double flippedHeight = m_channel[x, (int)m_channel.Height - 1 - y];
  939. m_channel[x, y] = flippedHeight;
  940. m_channel[x, (int)m_channel.Height - 1 - y] = height;
  941. }
  942. }
  943. }
  944. else if (direction.ToLower().StartsWith("x"))
  945. {
  946. for (int y = 0; y < m_channel.Height; y++)
  947. {
  948. for (int x = 0; x < m_channel.Width / 2; x++)
  949. {
  950. double height = m_channel[x, y];
  951. double flippedHeight = m_channel[(int)m_channel.Width - 1 - x, y];
  952. m_channel[x, y] = flippedHeight;
  953. m_channel[(int)m_channel.Width - 1 - x, y] = height;
  954. }
  955. }
  956. }
  957. else
  958. {
  959. m_log.Error("Unrecognised direction - need x or y");
  960. }
  961. }
  962. private void InterfaceRescaleTerrain(Object[] args)
  963. {
  964. double desiredMin = (double)args[0];
  965. double desiredMax = (double)args[1];
  966. // determine desired scaling factor
  967. double desiredRange = desiredMax - desiredMin;
  968. //m_log.InfoFormat("Desired {0}, {1} = {2}", new Object[] { desiredMin, desiredMax, desiredRange });
  969. if (desiredRange == 0d)
  970. {
  971. // delta is zero so flatten at requested height
  972. InterfaceFillTerrain(new Object[] { args[1] });
  973. }
  974. else
  975. {
  976. //work out current heightmap range
  977. double currMin = double.MaxValue;
  978. double currMax = double.MinValue;
  979. int width = m_channel.Width;
  980. int height = m_channel.Height;
  981. for (int x = 0; x < width; x++)
  982. {
  983. for (int y = 0; y < height; y++)
  984. {
  985. double currHeight = m_channel[x, y];
  986. if (currHeight < currMin)
  987. {
  988. currMin = currHeight;
  989. }
  990. else if (currHeight > currMax)
  991. {
  992. currMax = currHeight;
  993. }
  994. }
  995. }
  996. double currRange = currMax - currMin;
  997. double scale = desiredRange / currRange;
  998. //m_log.InfoFormat("Current {0}, {1} = {2}", new Object[] { currMin, currMax, currRange });
  999. //m_log.InfoFormat("Scale = {0}", scale);
  1000. // scale the heightmap accordingly
  1001. for (int x = 0; x < width; x++)
  1002. {
  1003. for (int y = 0; y < height; y++)
  1004. {
  1005. double currHeight = m_channel[x, y] - currMin;
  1006. m_channel[x, y] = desiredMin + (currHeight * scale);
  1007. }
  1008. }
  1009. }
  1010. }
  1011. private void InterfaceElevateTerrain(Object[] args)
  1012. {
  1013. int x, y;
  1014. for (x = 0; x < m_channel.Width; x++)
  1015. for (y = 0; y < m_channel.Height; y++)
  1016. m_channel[x, y] += (double) args[0];
  1017. }
  1018. private void InterfaceMultiplyTerrain(Object[] args)
  1019. {
  1020. int x, y;
  1021. for (x = 0; x < m_channel.Width; x++)
  1022. for (y = 0; y < m_channel.Height; y++)
  1023. m_channel[x, y] *= (double) args[0];
  1024. }
  1025. private void InterfaceLowerTerrain(Object[] args)
  1026. {
  1027. int x, y;
  1028. for (x = 0; x < m_channel.Width; x++)
  1029. for (y = 0; y < m_channel.Height; y++)
  1030. m_channel[x, y] -= (double) args[0];
  1031. }
  1032. private void InterfaceFillTerrain(Object[] args)
  1033. {
  1034. int x, y;
  1035. for (x = 0; x < m_channel.Width; x++)
  1036. for (y = 0; y < m_channel.Height; y++)
  1037. m_channel[x, y] = (double) args[0];
  1038. }
  1039. private void InterfaceMinTerrain(Object[] args)
  1040. {
  1041. int x, y;
  1042. for (x = 0; x < m_channel.Width; x++)
  1043. {
  1044. for (y = 0; y < m_channel.Height; y++)
  1045. {
  1046. m_channel[x, y] = Math.Max((double)args[0], m_channel[x, y]);
  1047. }
  1048. }
  1049. }
  1050. private void InterfaceMaxTerrain(Object[] args)
  1051. {
  1052. int x, y;
  1053. for (x = 0; x < m_channel.Width; x++)
  1054. {
  1055. for (y = 0; y < m_channel.Height; y++)
  1056. {
  1057. m_channel[x, y] = Math.Min((double)args[0], m_channel[x, y]);
  1058. }
  1059. }
  1060. }
  1061. private void InterfaceShowDebugStats(Object[] args)
  1062. {
  1063. double max = Double.MinValue;
  1064. double min = double.MaxValue;
  1065. double sum = 0;
  1066. int x;
  1067. for (x = 0; x < m_channel.Width; x++)
  1068. {
  1069. int y;
  1070. for (y = 0; y < m_channel.Height; y++)
  1071. {
  1072. sum += m_channel[x, y];
  1073. if (max < m_channel[x, y])
  1074. max = m_channel[x, y];
  1075. if (min > m_channel[x, y])
  1076. min = m_channel[x, y];
  1077. }
  1078. }
  1079. double avg = sum / (m_channel.Height * m_channel.Width);
  1080. m_log.Info("Channel " + m_channel.Width + "x" + m_channel.Height);
  1081. m_log.Info("max/min/avg/sum: " + max + "/" + min + "/" + avg + "/" + sum);
  1082. }
  1083. private void InterfaceEnableExperimentalBrushes(Object[] args)
  1084. {
  1085. if ((bool) args[0])
  1086. {
  1087. m_painteffects[StandardTerrainEffects.Revert] = new WeatherSphere();
  1088. m_painteffects[StandardTerrainEffects.Flatten] = new OlsenSphere();
  1089. m_painteffects[StandardTerrainEffects.Smooth] = new ErodeSphere();
  1090. }
  1091. else
  1092. {
  1093. InstallDefaultEffects();
  1094. }
  1095. }
  1096. private void InterfaceRunPluginEffect(Object[] args)
  1097. {
  1098. string firstArg = (string)args[0];
  1099. if (firstArg == "list")
  1100. {
  1101. m_log.Info("List of loaded plugins");
  1102. foreach (KeyValuePair<string, ITerrainEffect> kvp in m_plugineffects)
  1103. {
  1104. m_log.Info(kvp.Key);
  1105. }
  1106. return;
  1107. }
  1108. if (firstArg == "reload")
  1109. {
  1110. LoadPlugins();
  1111. return;
  1112. }
  1113. if (m_plugineffects.ContainsKey(firstArg))
  1114. {
  1115. m_plugineffects[firstArg].RunEffect(m_channel);
  1116. }
  1117. else
  1118. {
  1119. m_log.Warn("No such plugin effect loaded.");
  1120. }
  1121. }
  1122. private void InstallInterfaces()
  1123. {
  1124. Command loadFromFileCommand =
  1125. new Command("load", CommandIntentions.COMMAND_HAZARDOUS, InterfaceLoadFile, "Loads a terrain from a specified file.");
  1126. loadFromFileCommand.AddArgument("filename",
  1127. "The file you wish to load from, the file extension determines the loader to be used. Supported extensions include: " +
  1128. m_supportedFileExtensions, "String");
  1129. Command saveToFileCommand =
  1130. new Command("save", CommandIntentions.COMMAND_NON_HAZARDOUS, InterfaceSaveFile, "Saves the current heightmap to a specified file.");
  1131. saveToFileCommand.AddArgument("filename",
  1132. "The destination filename for your heightmap, the file extension determines the format to save in. Supported extensions include: " +
  1133. m_supportedFileExtensions, "String");
  1134. Command loadFromTileCommand =
  1135. new Command("load-tile", CommandIntentions.COMMAND_HAZARDOUS, InterfaceLoadTileFile, "Loads a terrain from a section of a larger file.");
  1136. loadFromTileCommand.AddArgument("filename",
  1137. "The file you wish to load from, the file extension determines the loader to be used. Supported extensions include: " +
  1138. m_supportedFileExtensions, "String");
  1139. loadFromTileCommand.AddArgument("file width", "The width of the file in tiles", "Integer");
  1140. loadFromTileCommand.AddArgument("file height", "The height of the file in tiles", "Integer");
  1141. loadFromTileCommand.AddArgument("minimum X tile", "The X region coordinate of the first section on the file",
  1142. "Integer");
  1143. loadFromTileCommand.AddArgument("minimum Y tile", "The Y region coordinate of the first section on the file",
  1144. "Integer");
  1145. Command saveToTileCommand =
  1146. new Command("save-tile", CommandIntentions.COMMAND_HAZARDOUS, InterfaceSaveTileFile, "Saves the current heightmap to the larger file.");
  1147. saveToTileCommand.AddArgument("filename",
  1148. "The file you wish to save to, the file extension determines the loader to be used. Supported extensions include: " +
  1149. m_supportFileExtensionsForTileSave, "String");
  1150. saveToTileCommand.AddArgument("file width", "The width of the file in tiles", "Integer");
  1151. saveToTileCommand.AddArgument("file height", "The height of the file in tiles", "Integer");
  1152. saveToTileCommand.AddArgument("minimum X tile", "The X region coordinate of the first section on the file",
  1153. "Integer");
  1154. saveToTileCommand.AddArgument("minimum Y tile", "The Y region coordinate of the first tile on the file\n"
  1155. + "= Example =\n"
  1156. + "To save a PNG file for a set of map tiles 2 regions wide and 3 regions high from map co-ordinate (9910,10234)\n"
  1157. + " # terrain save-tile ST06.png 2 3 9910 10234\n",
  1158. "Integer");
  1159. // Terrain adjustments
  1160. Command fillRegionCommand =
  1161. new Command("fill", CommandIntentions.COMMAND_HAZARDOUS, InterfaceFillTerrain, "Fills the current heightmap with a specified value.");
  1162. fillRegionCommand.AddArgument("value", "The numeric value of the height you wish to set your region to.",
  1163. "Double");
  1164. Command elevateCommand =
  1165. new Command("elevate", CommandIntentions.COMMAND_HAZARDOUS, InterfaceElevateTerrain, "Raises the current heightmap by the specified amount.");
  1166. elevateCommand.AddArgument("amount", "The amount of height to add to the terrain in meters.", "Double");
  1167. Command lowerCommand =
  1168. new Command("lower", CommandIntentions.COMMAND_HAZARDOUS, InterfaceLowerTerrain, "Lowers the current heightmap by the specified amount.");
  1169. lowerCommand.AddArgument("amount", "The amount of height to remove from the terrain in meters.", "Double");
  1170. Command multiplyCommand =
  1171. new Command("multiply", CommandIntentions.COMMAND_HAZARDOUS, InterfaceMultiplyTerrain, "Multiplies the heightmap by the value specified.");
  1172. multiplyCommand.AddArgument("value", "The value to multiply the heightmap by.", "Double");
  1173. Command bakeRegionCommand =
  1174. new Command("bake", CommandIntentions.COMMAND_HAZARDOUS, InterfaceBakeTerrain, "Saves the current terrain into the regions revert map.");
  1175. Command revertRegionCommand =
  1176. new Command("revert", CommandIntentions.COMMAND_HAZARDOUS, InterfaceRevertTerrain, "Loads the revert map terrain into the regions heightmap.");
  1177. Command flipCommand =
  1178. new Command("flip", CommandIntentions.COMMAND_HAZARDOUS, InterfaceFlipTerrain, "Flips the current terrain about the X or Y axis");
  1179. flipCommand.AddArgument("direction", "[x|y] the direction to flip the terrain in", "String");
  1180. Command rescaleCommand =
  1181. new Command("rescale", CommandIntentions.COMMAND_HAZARDOUS, InterfaceRescaleTerrain, "Rescales the current terrain to fit between the given min and max heights");
  1182. rescaleCommand.AddArgument("min", "min terrain height after rescaling", "Double");
  1183. rescaleCommand.AddArgument("max", "max terrain height after rescaling", "Double");
  1184. Command minCommand = new Command("min", CommandIntentions.COMMAND_HAZARDOUS, InterfaceMinTerrain, "Sets the minimum terrain height to the specified value.");
  1185. minCommand.AddArgument("min", "terrain height to use as minimum", "Double");
  1186. Command maxCommand = new Command("max", CommandIntentions.COMMAND_HAZARDOUS, InterfaceMaxTerrain, "Sets the maximum terrain height to the specified value.");
  1187. maxCommand.AddArgument("min", "terrain height to use as maximum", "Double");
  1188. // Debug
  1189. Command showDebugStatsCommand =
  1190. new Command("stats", CommandIntentions.COMMAND_STATISTICAL, InterfaceShowDebugStats,
  1191. "Shows some information about the regions heightmap for debugging purposes.");
  1192. Command experimentalBrushesCommand =
  1193. new Command("newbrushes", CommandIntentions.COMMAND_HAZARDOUS, InterfaceEnableExperimentalBrushes,
  1194. "Enables experimental brushes which replace the standard terrain brushes. WARNING: This is a debug setting and may be removed at any time.");
  1195. experimentalBrushesCommand.AddArgument("Enabled?", "true / false - Enable new brushes", "Boolean");
  1196. //Plugins
  1197. Command pluginRunCommand =
  1198. new Command("effect", CommandIntentions.COMMAND_HAZARDOUS, InterfaceRunPluginEffect, "Runs a specified plugin effect");
  1199. pluginRunCommand.AddArgument("name", "The plugin effect you wish to run, or 'list' to see all plugins", "String");
  1200. m_commander.RegisterCommand("load", loadFromFileCommand);
  1201. m_commander.RegisterCommand("load-tile", loadFromTileCommand);
  1202. m_commander.RegisterCommand("save", saveToFileCommand);
  1203. m_commander.RegisterCommand("save-tile", saveToTileCommand);
  1204. m_commander.RegisterCommand("fill", fillRegionCommand);
  1205. m_commander.RegisterCommand("elevate", elevateCommand);
  1206. m_commander.RegisterCommand("lower", lowerCommand);
  1207. m_commander.RegisterCommand("multiply", multiplyCommand);
  1208. m_commander.RegisterCommand("bake", bakeRegionCommand);
  1209. m_commander.RegisterCommand("revert", revertRegionCommand);
  1210. m_commander.RegisterCommand("newbrushes", experimentalBrushesCommand);
  1211. m_commander.RegisterCommand("stats", showDebugStatsCommand);
  1212. m_commander.RegisterCommand("effect", pluginRunCommand);
  1213. m_commander.RegisterCommand("flip", flipCommand);
  1214. m_commander.RegisterCommand("rescale", rescaleCommand);
  1215. m_commander.RegisterCommand("min", minCommand);
  1216. m_commander.RegisterCommand("max", maxCommand);
  1217. // Add this to our scene so scripts can call these functions
  1218. m_scene.RegisterModuleCommander(m_commander);
  1219. }
  1220. #endregion
  1221. }
  1222. }