TerrainModule.cs 57 KB

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