PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Commands/CmdFill.cs

https://github.com/cazzar/MCaznowl-Lava
C# | 255 lines | 204 code | 29 blank | 22 comment | 86 complexity | a984c6b71ef51ba237269b211ae371fd MD5 | raw file
  1. /*
  2. Copyright 2011 MCForge
  3. Dual-licensed under the Educational Community License, Version 2.0 and
  4. the GNU General Public License, Version 3 (the "Licenses"); you may
  5. not use this file except in compliance with the Licenses. You may
  6. obtain a copy of the Licenses at
  7. http://www.opensource.org/licenses/ecl2.php
  8. http://www.gnu.org/licenses/gpl-3.0.html
  9. Unless required by applicable law or agreed to in writing,
  10. software distributed under the Licenses are distributed on an "AS IS"
  11. BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  12. or implied. See the Licenses for the specific language governing
  13. permissions and limitations under the Licenses.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. namespace MCForge
  18. {
  19. public class CmdFill : Command
  20. {
  21. public override string name { get { return "fill"; } }
  22. public override string shortcut { get { return "f"; } }
  23. public override string type { get { return "build"; } }
  24. public override bool museumUsable { get { return false; } }
  25. public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
  26. public CmdFill() { }
  27. public override void Use(Player p, string message)
  28. {
  29. CatchPos cpos;
  30. int number = message.Split(' ').Length;
  31. if (number > 2) { Help(p); return; }
  32. if (number == 2)
  33. {
  34. int pos = message.IndexOf(' ');
  35. string t = message.Substring(0, pos).ToLower();
  36. string s = message.Substring(pos + 1).ToLower();
  37. cpos.type = Block.Byte(t);
  38. if (cpos.type == 255) { Player.SendMessage(p, "There is no block \"" + t + "\"."); return; }
  39. if (!Block.canPlace(p, cpos.type)) { Player.SendMessage(p, "Cannot place that."); return; }
  40. if (s == "up") cpos.fillType = FillType.Up;
  41. else if (s == "down") cpos.fillType = FillType.Down;
  42. else if (s == "layer") cpos.fillType = FillType.Layer;
  43. else if (s == "vertical_x") cpos.fillType = FillType.VerticalX;
  44. else if (s == "vertical_z") cpos.fillType = FillType.VerticalZ;
  45. else { Player.SendMessage(p, "Invalid fill type"); return; }
  46. }
  47. else if (message != "")
  48. {
  49. message = message.ToLower();
  50. if (message == "up") { cpos.fillType = FillType.Up; cpos.type = Block.Zero; }
  51. else if (message == "down") { cpos.fillType = FillType.Down; cpos.type = Block.Zero; }
  52. else if (message == "layer") { cpos.fillType = FillType.Layer; cpos.type = Block.Zero; }
  53. else if (message == "vertical_x") { cpos.fillType = FillType.VerticalX; cpos.type = Block.Zero; }
  54. else if (message == "vertical_z") { cpos.fillType = FillType.VerticalZ; cpos.type = Block.Zero; }
  55. else
  56. {
  57. cpos.type = Block.Byte(message);
  58. if (cpos.type == (byte)255) { Player.SendMessage(p, "Invalid block or fill type"); return; }
  59. if (!Block.canPlace(p, cpos.type)) { Player.SendMessage(p, "Cannot place that."); return; }
  60. cpos.fillType = FillType.Default;
  61. }
  62. }
  63. else
  64. {
  65. cpos.type = Block.Zero; cpos.fillType = FillType.Default;
  66. }
  67. cpos.x = 0; cpos.y = 0; cpos.z = 0; p.blockchangeObject = cpos;
  68. Player.SendMessage(p, "Destroy the block you wish to fill."); p.ClearBlockchange();
  69. p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
  70. }
  71. public override void Help(Player p)
  72. {
  73. Player.SendMessage(p, "/fill [block] [type] - Fills the area specified with [block].");
  74. Player.SendMessage(p, "[types] - up, down, layer, vertical_x, vertical_z");
  75. }
  76. public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type)
  77. {
  78. try
  79. {
  80. p.ClearBlockchange();
  81. CatchPos cpos = (CatchPos)p.blockchangeObject;
  82. if (cpos.type == Block.Zero) cpos.type = p.bindings[type];
  83. byte oldType = p.level.GetTile(x, y, z);
  84. p.SendBlockchange(x, y, z, oldType);
  85. if (cpos.type == oldType) { Player.SendMessage(p, "Cannot fill with the same type."); return; }
  86. if (!Block.canPlace(p, oldType) && !Block.BuildIn(oldType)) { Player.SendMessage(p, "Cannot fill with that."); return; }
  87. byte[] mapBlocks = new byte[p.level.blocks.Length];
  88. List<Pos> buffer = new List<Pos>();
  89. p.level.blocks.CopyTo(mapBlocks, 0);
  90. fromWhere.Clear();
  91. deep = 0;
  92. FloodFill(p, x, y, z, cpos.type, oldType, cpos.fillType, ref mapBlocks, ref buffer);
  93. int totalFill = fromWhere.Count;
  94. for (int i = 0; i < totalFill; i++)
  95. {
  96. totalFill = fromWhere.Count;
  97. Pos pos = fromWhere[i];
  98. deep = 0;
  99. FloodFill(p, pos.x, pos.y, pos.z, cpos.type, oldType, cpos.fillType, ref mapBlocks, ref buffer);
  100. totalFill = fromWhere.Count;
  101. }
  102. fromWhere.Clear();
  103. if (buffer.Count > p.group.maxBlocks)
  104. {
  105. Player.SendMessage(p, "You tried to fill " + buffer.Count + " blocks.");
  106. Player.SendMessage(p, "You cannot fill more than " + p.group.maxBlocks + ".");
  107. return;
  108. }
  109. foreach (Pos pos in buffer)
  110. {
  111. p.level.Blockchange(p, pos.x, pos.y, pos.z, cpos.type);
  112. }
  113. Player.SendMessage(p, "Filled " + buffer.Count + " blocks.");
  114. buffer.Clear();
  115. buffer = null;
  116. mapBlocks = null;
  117. if (p.staticCommands) p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
  118. }
  119. catch (Exception e)
  120. {
  121. Server.ErrorLog(e);
  122. }
  123. }
  124. int deep;
  125. List<Pos> fromWhere = new List<Pos>();
  126. public void FloodFill(Player p, ushort x, ushort y, ushort z, byte b, byte oldType, FillType fillType, ref byte[] blocks, ref List<Pos> buffer)
  127. {
  128. try
  129. {
  130. Pos pos;
  131. pos.x = x; pos.y = y; pos.z = z;
  132. if (deep > 4000)
  133. {
  134. fromWhere.Add(pos);
  135. return;
  136. }
  137. blocks[x + p.level.width * z + p.level.width * p.level.height * y] = b;
  138. buffer.Add(pos);
  139. //x
  140. if (fillType != FillType.VerticalX)
  141. {
  142. if (GetTile((ushort)(x + 1), y, z, p.level, blocks) == oldType)
  143. {
  144. deep++;
  145. FloodFill(p, (ushort)(x + 1), y, z, b, oldType, fillType, ref blocks, ref buffer);
  146. deep--;
  147. }
  148. if (x > 0)
  149. if (GetTile((ushort)(x - 1), y, z, p.level, blocks) == oldType)
  150. {
  151. deep++;
  152. FloodFill(p, (ushort)(x - 1), y, z, b, oldType, fillType, ref blocks, ref buffer);
  153. deep--;
  154. }
  155. }
  156. //z
  157. if (fillType != FillType.VerticalZ)
  158. {
  159. if (GetTile(x, y, (ushort)(z + 1), p.level, blocks) == oldType)
  160. {
  161. deep++;
  162. FloodFill(p, x, y, (ushort)(z + 1), b, oldType, fillType, ref blocks, ref buffer);
  163. deep--;
  164. }
  165. if (z > 0)
  166. if (GetTile(x, y, (ushort)(z - 1), p.level, blocks) == oldType)
  167. {
  168. deep++;
  169. FloodFill(p, x, y, (ushort)(z - 1), b, oldType, fillType, ref blocks, ref buffer);
  170. deep--;
  171. }
  172. }
  173. //y
  174. if (fillType == 0 || fillType == FillType.Up || fillType > FillType.Layer)
  175. {
  176. if (GetTile(x, (ushort)(y + 1), z, p.level, blocks) == oldType)
  177. {
  178. deep++;
  179. FloodFill(p, x, (ushort)(y + 1), z, b, oldType, fillType, ref blocks, ref buffer);
  180. deep--;
  181. }
  182. }
  183. if (fillType == 0 || fillType == FillType.Down || fillType > FillType.Layer)
  184. {
  185. if (y > 0)
  186. if (GetTile(x, (ushort)(y - 1), z, p.level, blocks) == oldType)
  187. {
  188. deep++;
  189. FloodFill(p, x, (ushort)(y - 1), z, b, oldType, fillType, ref blocks, ref buffer);
  190. deep--;
  191. }
  192. }
  193. } catch (Exception e) { Server.ErrorLog(e); }
  194. }
  195. public byte GetTile(ushort x, ushort y, ushort z, Level l, byte[] blocks)
  196. {
  197. //if (PosToInt(x, y, z) >= blocks.Length) { return null; }
  198. //Avoid internal overflow
  199. if (x < 0) { return Block.Zero; }
  200. if (x >= l.width) { return Block.Zero; }
  201. if (y < 0) { return Block.Zero; }
  202. if (y >= l.depth) { return Block.Zero; }
  203. if (z < 0) { return Block.Zero; }
  204. if (z >= l.height) { return Block.Zero; }
  205. try
  206. {
  207. return blocks[l.PosToInt(x, y, z)];
  208. }
  209. catch (Exception e) { Server.ErrorLog(e); return Block.Zero; }
  210. }
  211. struct CatchPos { public ushort x, y, z; public byte type; public FillType fillType; }
  212. public struct Pos { public ushort x, y, z; }
  213. public enum FillType : int
  214. {
  215. Default = 0,
  216. Up = 1,
  217. Down = 2,
  218. Layer = 3,
  219. VerticalX = 4,
  220. VerticalZ = 5
  221. }
  222. }
  223. }