Guest User

Untitled

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