/src/WallGen.java

https://github.com/Theino/Towny-Obsidian · Java · 294 lines · 219 code · 29 blank · 46 comment · 76 complexity · c401102ae7adbf09ce8d3d93433ac38b MD5 · raw file

  1. import java.util.*;
  2. import java.util.logging.Logger;
  3. public class WallGen {
  4. public static final int topSection = 4; //How many layers the top part of the wall is.
  5. protected static final Logger log = Logger.getLogger("Minecraft");
  6. /*
  7. WallBlock type:
  8. 0 = Wall
  9. 1 = Turret wall (3 enemy sides)
  10. 2 = Turret wall (1 enemy sides)
  11. */
  12. public static boolean deleteTownWall(TownyWorld world, Town town) {
  13. BlockQueue blockQueue = BlockQueue.getInstance();
  14. //Delete old wall
  15. //ArrayList<TownBlock> townblocks = world.getTownBlocks(town);
  16. for (WallBlock wb : town.wall.sections) {
  17. for (int z = 0; z < 4; z++) {
  18. //for (int y = 0; y < town.wall.walkwayHeight+topSection-(int)wb.p.y; y++) {
  19. for (int y = 0; y < town.wall.height+topSection; y++) {
  20. for (int x = 0; x < 4; x++) {
  21. blockQueue.addWork(new Block(0, (int)wb.p.x+x, (int)wb.p.y+y, (int)wb.p.z+z));
  22. }
  23. }
  24. }
  25. }
  26. town.wall.sections.clear();
  27. return true;
  28. }
  29. public static boolean townGen(TownyWorld world, Town town) {
  30. ArrayList<TownBlock> townblocks = world.getTownBlocks(town);
  31. // Assign Turrets
  32. for (TownBlock tb : townblocks) {
  33. if (!isPartOfTown(tb.x-1, tb.z, town)) {
  34. for (int z = 0; z < TownyProperties.blockSize; z += 4)
  35. town.wall.sections.add(new WallBlock(lowestInGrid(tb.x*TownyProperties.blockSize, z+tb.z*TownyProperties.blockSize, 4, 4), 0, 0));
  36. }
  37. if (!isPartOfTown(tb.x, tb.z-1, town)) {
  38. for (int x = 0; x < TownyProperties.blockSize; x += 4)
  39. town.wall.sections.add(new WallBlock(lowestInGrid(x+tb.x*TownyProperties.blockSize, tb.z*TownyProperties.blockSize, 4, 4), 3, 0));
  40. }
  41. if (!isPartOfTown(tb.x+1, tb.z, town)) {
  42. for (int z = 0; z < TownyProperties.blockSize; z += 4)
  43. town.wall.sections.add(new WallBlock(lowestInGrid(tb.x*TownyProperties.blockSize+(TownyProperties.blockSize-4), z+tb.z*TownyProperties.blockSize, 4, 4), 2, 0));
  44. }
  45. if (!isPartOfTown(tb.x, tb.z+1, town)) {
  46. for (int x = 0; x < TownyProperties.blockSize; x += 4)
  47. town.wall.sections.add(new WallBlock(lowestInGrid(x+tb.x*TownyProperties.blockSize, tb.z*TownyProperties.blockSize+(TownyProperties.blockSize-4), 4, 4), 1, 0));
  48. }
  49. // x = current townblock, f = town, e = else
  50. // ef ee
  51. // fx or ex
  52. if (!isPartOfTown(tb.x-1, tb.z+1, town) &&
  53. (isPartOfTown(tb.x-1, tb.z, town) && isPartOfTown(tb.x, tb.z+1, town)) ||
  54. (!isPartOfTown(tb.x-1, tb.z, town) && !isPartOfTown(tb.x, tb.z+1, town)) )
  55. town.wall.sections.add(new WallBlock(lowestInGrid(tb.x*TownyProperties.blockSize, tb.z*TownyProperties.blockSize+(TownyProperties.blockSize-4), 4, 4), 1, (isPartOfTown(tb.x-1, tb.z, town) && isPartOfTown(tb.x, tb.z+1, town)) ? 2 : 1));
  56. // fe ee
  57. // xf or xe
  58. if (!isPartOfTown(tb.x+1, tb.z+1, town) &&
  59. (isPartOfTown(tb.x+1, tb.z, town) && isPartOfTown(tb.x, tb.z+1, town)) ||
  60. (!isPartOfTown(tb.x+1, tb.z, town) && !isPartOfTown(tb.x, tb.z+1, town)) )
  61. town.wall.sections.add(new WallBlock(lowestInGrid(tb.x*TownyProperties.blockSize+(TownyProperties.blockSize-4), tb.z*TownyProperties.blockSize+(TownyProperties.blockSize-4), 4, 4), 2, (isPartOfTown(tb.x+1, tb.z, town) && isPartOfTown(tb.x, tb.z+1, town)) ? 2 : 1));
  62. // xf xe
  63. // fe or ee
  64. if (!isPartOfTown(tb.x+1, tb.z-1, town) &&
  65. (isPartOfTown(tb.x+1, tb.z, town) && isPartOfTown(tb.x, tb.z-1, town)) ||
  66. (!isPartOfTown(tb.x+1, tb.z, town) && !isPartOfTown(tb.x, tb.z-1, town)) )
  67. town.wall.sections.add(new WallBlock(lowestInGrid(tb.x*TownyProperties.blockSize+(TownyProperties.blockSize-4), tb.z*TownyProperties.blockSize, 4, 4), 3, (isPartOfTown(tb.x+1, tb.z, town) && isPartOfTown(tb.x, tb.z-1, town)) ? 2 : 1));
  68. // fx ex
  69. // ef or ee
  70. if (!isPartOfTown(tb.x-1, tb.z-1, town) &&
  71. (isPartOfTown(tb.x-1, tb.z, town) && isPartOfTown(tb.x, tb.z-1, town)) ||
  72. (!isPartOfTown(tb.x-1, tb.z, town) && !isPartOfTown(tb.x, tb.z-1, town)) )
  73. town.wall.sections.add(new WallBlock(lowestInGrid(tb.x*TownyProperties.blockSize, tb.z*TownyProperties.blockSize, 4, 4), 0, (isPartOfTown(tb.x-1, tb.z, town) && isPartOfTown(tb.x, tb.z-1, town)) ? 2 : 1));
  74. //TODO: delete walls where turrets are. Don't
  75. }
  76. /*
  77. for (WallBlock wb : town.wall.sections) {
  78. if ((int)wb.p.y > town.wall.walkwayHeight)
  79. town.wall.walkwayHeight = (int)wb.p.y+town.wall.height+3;
  80. }
  81. */
  82. for (WallBlock wb : town.wall.sections)
  83. drawWallBlock(town.wall, wb);
  84. return true;
  85. }
  86. public static void drawWallBlock(Wall wall, WallBlock wb) {
  87. BlockQueue blockQueue = BlockQueue.getInstance();
  88. // Even wall height
  89. //int[][][] arr = getBuildArray(wall.blockType, wall.walkwayHeight-(int)wb.p.y, wb.t, wb.r);
  90. int[][][] arr = getBuildArray(wall.blockType, wall.height, wb.t, wb.r);
  91. ArrayList<Block> addAfter = new ArrayList<Block>();
  92. for (int z = 0; z < 4; z++) {
  93. for (int y = 0; y < arr.length; y++) {
  94. for (int x = 0; x < 4; x++) {
  95. int bid = arr[y][x][z];
  96. if (bid == 65) {
  97. int ladderRot = 2;
  98. if (wb.r == 0) ladderRot = 3;
  99. else if (wb.r == 1) ladderRot = 4;
  100. else if (wb.r == 2) ladderRot = 2;
  101. else if (wb.r == 3) ladderRot = 5;
  102. addAfter.add(new Block(bid, (int)wb.p.x+x, (int)wb.p.y+y, (int)wb.p.z+z, ladderRot));
  103. } else {
  104. blockQueue.addWork(new Block(bid, (int)wb.p.x+x, (int)wb.p.y+y, (int)wb.p.z+z));
  105. }
  106. }
  107. }
  108. }
  109. for (Block block : addAfter)
  110. blockQueue.addWork(block);
  111. }
  112. public static Point lowestInGrid(long x, long z, int w, int h) {
  113. int y;
  114. int lowest = 127;
  115. for (long iz = z; iz < z+h; iz++) {
  116. for (long ix = x; ix < x+w; ix++) {
  117. y = etc.getServer().getHighestBlockY((int)ix, (int)iz);
  118. if (y < lowest)
  119. lowest = y;
  120. }
  121. }
  122. return new Point(x, lowest, z);
  123. }
  124. public static boolean isPartOfTown(long x, long z, Town town) {
  125. String key = x + "," + z;
  126. TownBlock tb = TownyWorld.getInstance().townblocks.get(key);
  127. if (tb != null && tb.town != null && tb.town == town)
  128. return true;
  129. return false;
  130. }
  131. public static int[][][] getBuildArray(int b, int h, int type, int rotation) {
  132. int s = 65; // Ladder
  133. int[][][] buildArray = {
  134. // Wall section
  135. {
  136. { 0, 0, 0, 0 },
  137. { b, b, b, b },
  138. { b, b, b, b },
  139. { 0, 0, 0, 0 }
  140. },
  141. // Wall walkway
  142. {
  143. { b, b, b, b },
  144. { b, b, b, b },
  145. { b, b, b, b },
  146. { b, b, b, b }
  147. },
  148. // Wall section walkway wall
  149. {
  150. { b, b, b, b },
  151. { 0, 0, 0, 0 },
  152. { 0, 0, 0, 0 },
  153. { b, b, b, b }
  154. },
  155. // Wall section walkway wall crenelation
  156. {
  157. { 44, 0, 0, 44 },
  158. { 0, 0, 0, 0 },
  159. { 0, 0, 0, 0 },
  160. { 44, 0, 0, 44 }
  161. },
  162. // Turret wall (3 enemy sides)
  163. {
  164. { 0, 0, 0, 0 },
  165. { 0, b, b, b },
  166. { 0, b, s, 0 },
  167. { 0, b, 0, 0 }
  168. },
  169. // Turret wall (1 enemy side)
  170. {
  171. { 0, b, b, 0 },
  172. { b, b, b, 0 },
  173. { b, b, s, 0 },
  174. { 0, 0, 0, 0 }
  175. },
  176. // Turret walkway
  177. {
  178. { b, b, b, b },
  179. { b, b, b, b },
  180. { b, b, s, b },
  181. { b, b, b, b }
  182. },
  183. // Turret wall (3 enemies) (is rotated twice to match (1 enemy)
  184. {
  185. { b, b, b, b },
  186. { b, 0, 0, 0 },
  187. { b, 0, 0, 0 },
  188. { b, 0, 0, b }
  189. },
  190. // Wall section Afv. crenels
  191. {
  192. { b, 0, 0, b },
  193. { b, b, b, b },
  194. { b, b, b, b },
  195. { 0, 0, 0, 0 }
  196. },
  197. };
  198. ArrayList<int[][]> out = new ArrayList<int[][]>();
  199. int[][] layer;
  200. // Walkway
  201. if (type == 0) {
  202. layer = rotate(buildArray[0], rotation);
  203. for (int i = 0; i < h; i++)
  204. out.add(layer);
  205. // Top Section
  206. out.add(rotate(buildArray[8], rotation));
  207. out.add(buildArray[1]); //No need to rotate the walkway
  208. out.add(rotate(buildArray[2], rotation));
  209. out.add(rotate(buildArray[3], rotation));
  210. }
  211. // Turret wall (3 enemy sides)
  212. else if (type == 1) {
  213. layer = rotate(buildArray[4], rotation);
  214. for (int i = 0; i < h; i++)
  215. out.add(layer);
  216. // Top Section
  217. layer = rotate(buildArray[6], rotation);
  218. for (int i=0;i<2;i++)
  219. out.add(layer);
  220. out.add(rotate(buildArray[7], rotation));
  221. out.add(rotate(buildArray[3], rotation));
  222. }
  223. // Turret wall (1 enemy sides)
  224. else if (type == 2) {
  225. layer = rotate(buildArray[5], rotation);
  226. for (int i = 0; i < h; i++)
  227. out.add(layer);
  228. // Top Section
  229. layer = rotate(buildArray[6], rotation);
  230. for (int i=0;i<2;i++)
  231. out.add(layer);
  232. out.add(rotate(buildArray[7], rotation+2)); //rotate wall to fit
  233. out.add(rotate(buildArray[3], rotation));
  234. }
  235. int[][][] toReturn = new int[out.size()][4][4];
  236. for (int z = 0; z < 4; z++) {
  237. for (int y = 0; y < out.size(); y++) {
  238. for (int x = 0; x < 4; x++) {
  239. toReturn[y][x][z] = out.get(y)[x][z];
  240. }
  241. }
  242. }
  243. return toReturn;
  244. //return (int[][][])out.toArray();
  245. }
  246. public static int[][] rotate(int[][] arr, int times) {
  247. int[][] temp = arr.clone();
  248. for (int i = 0; i < times; i++)
  249. temp = rotateMatrixRight(temp);
  250. return temp;
  251. }
  252. // http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
  253. public static int[][] rotateMatrixRight(int[][] matrix) {
  254. /* W and H are already swapped */
  255. int w = matrix.length;
  256. int h = matrix[0].length;
  257. int[][] ret = new int[h][w];
  258. for (int i = 0; i < h; ++i) {
  259. for (int j = 0; j < w; ++j) {
  260. ret[i][j] = matrix[w - j - 1][i];
  261. }
  262. }
  263. return ret;
  264. }
  265. }