TerrainModule.cs 56 KB

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