/Commands/CmdEllipse.cs

https://github.com/cazzar/MCaznowl-Build · C# · 365 lines · 272 code · 71 blank · 22 comment · 79 complexity · 93091ed0974964a361893ac2473d9e6d 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. using MCForge;
  18. namespace MCForge.Commands
  19. {
  20. public class CmdEllipse : Command
  21. {
  22. public override string name { get { return "ellipse"; } }
  23. public override string shortcut { get { return "el"; } }
  24. public override string type { get { return "build"; } }
  25. public override bool museumUsable { get { return false; } }
  26. public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } }
  27. public CmdEllipse() { }
  28. public override void Use(Player p, string message)
  29. {
  30. if (message != "") { Help(p); return; }
  31. Player.SendMessage(p, "Place two blocks to determine the edges.");
  32. p.ClearBlockchange();
  33. p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
  34. }
  35. public override void Help(Player p)
  36. {
  37. Player.SendMessage(p, "/ellipse - creates an ellipse.");
  38. }
  39. public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type)
  40. {
  41. p.ClearBlockchange();
  42. byte block = p.level.GetTile(x, y, z);
  43. p.SendBlockchange(x, y, z, block);
  44. Position bp;
  45. bp.x = x; bp.y = y; bp.z = z; bp.type = type; p.blockchangeObject = bp;
  46. p.Blockchange += new Player.BlockchangeEventHandler(Blockchange2);
  47. }
  48. public void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type)
  49. {
  50. p.ClearBlockchange();
  51. byte block = p.level.GetTile(x, y, z);
  52. p.SendBlockchange(x, y, z, block);
  53. Position cpos = (Position)p.blockchangeObject;
  54. unchecked { if (cpos.type != (byte)-1) { type = cpos.type; } }
  55. double x1 = cpos.x;
  56. double y1 = cpos.z;
  57. double x2 = x;
  58. double y2 = z;
  59. int height = Math.Abs(cpos.y - y) + 1;
  60. double xstart = Math.Min(x1, x2);
  61. double ystart = Math.Min(y1, y2);
  62. double a = ((Math.Abs(x1 - x2) + 1) / 2);
  63. double b = ((Math.Abs(y1 - y2) + 1) / 2);
  64. int dimensionx = (int)(Math.Abs(x1 - x2) + 1);
  65. bool OVX;
  66. bool OVY;
  67. int dimensiony = (int)(Math.Abs(y1 - y2) + 1);
  68. double[] yc = new double[dimensionx / 2 + 1];
  69. double[] length = new double[dimensionx / 2 + 1];
  70. if (dimensionx % 2 == 0) // x is even
  71. {
  72. OVX = false;
  73. }
  74. else
  75. {
  76. // It's odd
  77. OVX = true;
  78. }
  79. if (dimensiony % 2 == 0)
  80. {
  81. OVY = false;
  82. }
  83. else { OVY = true; }
  84. int limit = 0;
  85. for (int i = 0; i < ((int)a); i++)
  86. {
  87. if (i == 0)
  88. {
  89. yc[i] = Math.Ceiling(Math.Abs(Math.Sqrt((Math.Pow(a, 2) - Math.Pow(i + 0.5, 2))) * Math.Abs(b / a)));
  90. length[i] = 0;
  91. limit++;
  92. }
  93. else
  94. {
  95. if (i == (int)a - 1)
  96. {
  97. yc[i] = 1;
  98. if (yc[i - 1] - yc[i] > 1)
  99. {
  100. length[i] = Math.Round(yc[i - 1]) - Math.Round(yc[i]) - 1;
  101. limit++;
  102. }
  103. else { length[i] = 0; limit++; }
  104. }
  105. else
  106. {
  107. yc[i] = Math.Abs(Math.Sqrt((Math.Pow(a, 2) - Math.Pow(i + 0.5, 2))) * Math.Abs(b / a));
  108. if (yc[i - 1] - yc[i] > 1)
  109. {
  110. length[i] = Math.Round(yc[i - 1]) - Math.Round(yc[i]) - 1; limit++;
  111. }
  112. else { length[i] = 0; limit++; }
  113. }
  114. }
  115. }
  116. if ((limit * height) > p.group.maxBlocks)
  117. {
  118. Player.SendMessage(p, "You tried to place " + (limit * height) + " blocks.");
  119. Player.SendMessage(p, "You cannot replace more than " + p.group.maxBlocks + ".");
  120. return;
  121. }
  122. //firststart
  123. int startx = (int)xstart + (int)a;
  124. int starty = (int)ystart + (int)b;
  125. ushort starth = Math.Min(cpos.y, y);
  126. //int endh = Math.Max(cpos.y,y);
  127. for (int h = 0; h < height; h++)
  128. {
  129. for (int i = 0; i < ((int)a); i++)
  130. {
  131. if (length[i] == 0)
  132. {
  133. p.level.Blockchange(p, (ushort)(startx + i), starth, (ushort)(starty + ((int)Math.Round(yc[i]) - 1)), type);
  134. }
  135. if (length[i] != 0)
  136. {
  137. for (int ii = 0; ii <= length[i]; ii++)
  138. {
  139. p.level.Blockchange(p, (ushort)(startx + i), starth, (ushort)(starty + ((int)Math.Round(yc[i]) - 1 + ii)), type);
  140. }
  141. }
  142. }
  143. if (OVX)
  144. {
  145. for (int i = 0; i < ((int)a); i++)
  146. {
  147. if (length[i] == 0)
  148. {
  149. p.level.Blockchange(p, (ushort)(startx - i), starth, (ushort)(starty + ((int)Math.Round(yc[i]) - 1)), type);
  150. }
  151. if (length[i] != 0)
  152. {
  153. for (int ii = 0; ii <= length[i]; ii++)
  154. {
  155. p.level.Blockchange(p, (ushort)(startx - i), starth, (ushort)(starty + ((int)Math.Round(yc[i]) - 1 + ii)), type);
  156. }
  157. }
  158. }
  159. }
  160. else
  161. {
  162. for (int i = 0; i < ((int)a); i++)
  163. {
  164. if (length[i] == 0)
  165. {
  166. p.level.Blockchange(p, (ushort)(startx - 1 - i), starth, (ushort)(starty + ((int)Math.Round(yc[i]) - 1)), type);
  167. }
  168. if (length[i] != 0)
  169. {
  170. for (int ii = 0; ii <= length[i]; ii++)
  171. {
  172. p.level.Blockchange(p, (ushort)(startx - 1 - i), starth, (ushort)(starty + ((int)Math.Round(yc[i]) - 1 + ii)), type);
  173. }
  174. }
  175. }
  176. }
  177. // OVY STARTS HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  178. if (OVY)
  179. {
  180. for (int i = 0; i < ((int)a); i++)
  181. {
  182. if (length[i] == 0)
  183. {
  184. p.level.Blockchange(p, (ushort)(startx + i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) - 1)), type);
  185. }
  186. if (length[i] != 0)
  187. {
  188. for (int ii = 0; ii <= length[i]; ii++)
  189. {
  190. p.level.Blockchange(p, (ushort)(startx + i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) - 1 + ii)), type);
  191. }
  192. }
  193. }
  194. if (OVX)
  195. {
  196. for (int i = 0; i < ((int)a); i++)
  197. {
  198. if (length[i] == 0)
  199. {
  200. p.level.Blockchange(p, (ushort)(startx - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) - 1)), type);
  201. }
  202. if (length[i] != 0)
  203. {
  204. for (int ii = 0; ii <= length[i]; ii++)
  205. {
  206. p.level.Blockchange(p, (ushort)(startx - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) - 1 + ii)), type);
  207. }
  208. }
  209. }
  210. }
  211. else
  212. {
  213. for (int i = 0; i < ((int)a); i++)
  214. {
  215. if (length[i] == 0)
  216. {
  217. p.level.Blockchange(p, (ushort)(startx - 1 - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) - 1)), type);
  218. }
  219. if (length[i] != 0)
  220. {
  221. for (int ii = 0; ii <= length[i]; ii++)
  222. {
  223. p.level.Blockchange(p, (ushort)(startx - 1 - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) - 1 + ii)), type);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. // NO OVY !!!!!!!!!!!!!!!!!!!!!!!!
  230. else
  231. {
  232. for (int i = 0; i < ((int)a); i++)
  233. {
  234. if (length[i] == 0)
  235. {
  236. p.level.Blockchange(p, (ushort)(startx + i), starth, (ushort)(starty - ((int)Math.Round(yc[i]))), type);
  237. }
  238. if (length[i] != 0)
  239. {
  240. for (int ii = 0; ii <= length[i]; ii++)
  241. {
  242. p.level.Blockchange(p, (ushort)(startx + i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) + ii)), type);
  243. }
  244. }
  245. }
  246. if (OVX)
  247. {
  248. for (int i = 0; i < ((int)a); i++)
  249. {
  250. if (length[i] == 0)
  251. {
  252. p.level.Blockchange(p, (ushort)(startx - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]))), type);
  253. }
  254. if (length[i] != 0)
  255. {
  256. for (int ii = 0; ii <= length[i]; ii++)
  257. {
  258. p.level.Blockchange(p, (ushort)(startx - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) + ii)), type);
  259. }
  260. }
  261. }
  262. }
  263. else
  264. {
  265. for (int i = 0; i < ((int)a); i++)
  266. {
  267. if (length[i] == 0)
  268. {
  269. p.level.Blockchange(p, (ushort)(startx - 1 - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]))), type);
  270. }
  271. if (length[i] != 0)
  272. {
  273. for (int ii = 0; ii <= length[i]; ii++)
  274. {
  275. p.level.Blockchange(p, (ushort)(startx - 1 - i), starth, (ushort)(starty - ((int)Math.Round(yc[i]) + ii)), type);
  276. }
  277. }
  278. }
  279. }
  280. }
  281. starth++;
  282. }
  283. if (p.staticCommands) p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1);
  284. }
  285. struct Position
  286. {
  287. public byte type;
  288. public ushort x, y, z;
  289. }
  290. }
  291. }