armark1ng

Untitled

Jun 25th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. package com.rs.game.map;
  2.  
  3. import com.rs.utils.Utils;
  4.  
  5. /*
  6. * @author Dragonkk/Alex
  7. */
  8. public class MapUtils {
  9.  
  10. private static interface StructureEncoder {
  11.  
  12. public abstract int encode(int x, int y, int plane);
  13.  
  14. }
  15.  
  16. private static interface StructureDecoder {
  17.  
  18. public abstract int[] decode(int id);
  19.  
  20. }
  21.  
  22. public static enum Structure {
  23.  
  24. TILE(null, 1, 1, new StructureEncoder() {
  25. @Override
  26. public int encode(int x, int y, int plane) {
  27. return y | (x << 14) | (plane << 28);
  28. }
  29. }, new StructureDecoder() {
  30. @Override
  31. public int[] decode(int id) {
  32. return new int[]{id >> 14 & 16383, id & 16383, id >> 28 & 3};
  33. }
  34. }), CHUNK(TILE, 8, 8, new StructureEncoder() {
  35. @Override
  36. public int encode(int x, int y, int plane) {
  37. return (x << 14) | (y << 3) | (plane << 24);
  38. }
  39. }, new StructureDecoder() {
  40. @Override
  41. public int[] decode(int id) {
  42. return new int[]{id >> 14 & 2047, id >> 3 & 2047, id >> 24 & 3};
  43. }
  44. }), REGION(CHUNK, 8, 8, new StructureEncoder() {
  45. @Override
  46. public int encode(int x, int y, int plane) {
  47. return ((x << 8) | y | (plane << 16));
  48. }
  49. }, new StructureDecoder() {
  50. @Override
  51. public int[] decode(int id) {
  52. return new int[]{id >> 8 & 255, id & 255, id >> 24 & 3};
  53. }
  54. }), MAP(REGION, 256, 256);
  55.  
  56. private Structure child;
  57. private int width, height;
  58. private StructureEncoder encoder;
  59. private StructureDecoder decoder;
  60.  
  61. /*
  62. * width * height squares.
  63. * For instance 4x4:
  64. * S S S S
  65. * S S S S
  66. * S S S S
  67. */
  68.  
  69. private Structure(Structure child, int width, int height, StructureEncoder encode, StructureDecoder decoder) {
  70. this.child = child;
  71. this.width = width;
  72. this.height = height;
  73. this.encoder = encode;
  74. this.decoder = decoder;
  75. }
  76.  
  77. private Structure(Structure child, int width, int height) {
  78. this(child, width, height, null, null);
  79. }
  80.  
  81. public int getWidth() {
  82. int x = width;
  83. Structure nextChild = child;
  84. while (nextChild != null) {
  85. x *= nextChild.width;
  86. nextChild = nextChild.child;
  87. }
  88. return x;
  89. }
  90.  
  91. public int getChildWidth() {
  92. return width;
  93. }
  94.  
  95. public int getHeight() {
  96. int y = height;
  97. Structure nextChild = child;
  98. while (nextChild != null) {
  99. y *= nextChild.height;
  100. nextChild = nextChild.child;
  101. }
  102. return y;
  103. }
  104.  
  105. public int encode(int x, int y) {
  106. return encode(x, y, 0);
  107. }
  108.  
  109. public int[] decode(int id) {
  110. return decoder == null ? null : decoder.decode(id);
  111. }
  112.  
  113. public int encode(int x, int y, int plane) {
  114. return encoder == null ? -1 : encoder.encode(x, y, plane);
  115. }
  116.  
  117. public int getChildHeight() {
  118. return width;
  119. }
  120.  
  121. @Override
  122. public String toString() {
  123. return Utils.formatPlayerNameForDisplay(name());
  124. }
  125. }
  126.  
  127. public static final class Area {
  128.  
  129. private Structure structure;
  130. private int x, y, width, height;
  131.  
  132. public Area(Structure structure, int x, int y, int width, int height) {
  133. this.structure = structure;
  134. this.x = x;
  135. this.y = y;
  136. this.width = width;
  137. this.height = height;
  138. }
  139.  
  140. public int getX() {
  141. return x;
  142. }
  143.  
  144. public int getY() {
  145. return y;
  146. }
  147.  
  148. public int getMapX() {
  149. return x * structure.getWidth();
  150. }
  151.  
  152. public int getMapY() {
  153. return y * structure.getHeight();
  154. }
  155.  
  156. public int getMapWidth() {
  157. return width * structure.getWidth();
  158. }
  159.  
  160. public int getMapHeight() {
  161. return height * structure.getHeight();
  162. }
  163.  
  164. public Structure getStructure() {
  165. return structure;
  166. }
  167.  
  168. @Override
  169. public int hashCode() {
  170. return structure.encode(x, y, 0);
  171. }
  172.  
  173. @Override
  174. public String toString() {
  175. return "Structure: " + structure.toString() + ", x: " + x + ", y: " + y + ", width: " + width + ", height: " + height;
  176. }
  177. }
  178.  
  179. public static Area getArea(int minX, int minY, int maxX, int maxY) {
  180. return getArea(Structure.TILE, minX, minY, maxX, maxY);
  181. }
  182.  
  183. public static Area getArea(Structure structure, int minX, int minY, int maxX, int maxY) {
  184. return new Area(structure, minX, minY, maxX - minY, maxY - minY);
  185. }
  186.  
  187. /*
  188. * returns converted area
  189. */
  190. public static Area convert(Structure to, Area area) {
  191. int x = area.getMapX() / to.getWidth();
  192. int y = area.getMapY() / to.getHeight();
  193. int width = area.getMapWidth() / to.getWidth();
  194. int height = area.getMapHeight() / to.getHeight();
  195. return new Area(to, x, y, width, height);
  196. }
  197.  
  198. /*
  199. * converted pos
  200. * return converted x and y
  201. */
  202. public static int[] convert(Structure from, Structure to, int... xy) {
  203. return new int[]
  204. { xy[0] * from.getWidth() / to.getWidth(), xy[1] * from.getHeight() / to.getHeight() };
  205. }
  206.  
  207. public static int encode(Structure structure, int... xyp) {
  208. return structure.encode(xyp[0], xyp[1], xyp.length == 3 ? xyp[2] : 0);
  209. }
  210.  
  211. public static int[] decode(Structure structure, int id) {
  212. return structure.decode(id);
  213. }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment