Thread: Regions

Results 1 to 4 of 4
  1. #1 Regions 
    Community Veteran

    WH:II:DOW's Avatar
    Join Date
    Dec 2007
    Age
    32
    Posts
    2,022
    Thanks given
    145
    Thanks received
    871
    Rep Power
    4275
    Download this: [Only registered and activated users can see links. ]

    Add this:

    Code:
    import java.io.RandomAccessFile;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel.MapMode;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class Region {
    
    	private static MappedByteBuffer buffer;
    
    	private static final Region[] regions = new Region[20];
    	private static final int[][] indices = new int[474][1290];
    
    	private final List<GameObject> objects = new ArrayList<GameObject>();
    
    	private int x;
    	private int y;
    
    	private static int index;
    
    	public Region(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    
    	public static void initialize() throws Exception {
    		RandomAccessFile raf =  new RandomAccessFile("data/worldmap.dat", "r");
    		buffer = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
    		RandomAccessFile idxFile = new RandomAccessFile("data/worldmap.idx", "r");
    		MappedByteBuffer idxBuffer = idxFile.getChannel().map(MapMode.READ_ONLY, 0, idxFile.length());
    		while(idxBuffer.hasRemaining()) {
    			int x = idxBuffer.getShort();
    			int y = idxBuffer.getShort();
    			int offset = idxBuffer.getInt();
    			indices[x][y] = offset;
    		}
    	}
    
    	public static Region loadRegion(int x, int y) {
    		x = (x >> 3) - 6;
    		y = (y >> 3) - 6;
    		for(Region region : regions) {
    			if(region != null && region.getX() == x && region.getY() == y) {
    				return region;
    			}
    		}
    		index = (index + 1) % regions.length;
    		Region region = new Region(x, y);
    		loadRegion(region);
    		regions[index] = region;
    		return region;
    	}
    
    	public GameObject getObject(int x, int y) {
    		for(Iterator<GameObject> it$ = objects.iterator(); it$.hasNext(); ){
    			GameObject obj = it$.next();
    			if(obj.getX() == x && obj.getY() == y) {
    				return obj;
    			}
    		}
    		return null;
    	}
    
    	private static void loadRegion(Region region) {
    		buffer.position(indices[region.getX()][region.getY()]);
    		while(true) {
    			int opcode = buffer.get() & 0xFF;
    			if(opcode == 0) {
    				break;
    			} else if(opcode == 1) {
    				int id = buffer.getShort();
    				int x = buffer.getShort();
    				int y = buffer.getShort();
    				int height = buffer.get();
    				int type = buffer.get();
    				int face = buffer.get();
    				GameObject obj = new GameObject(id, type, x, y, face, height);
    				region.getObjects().add(obj);
    			}
    		}
    	}
    
    	public void setX(int x) {
    		this.x = x;
    	}
    
    	public int getX() {
    		return x;
    	}
    
    	public void setY(int y) {
    		this.y = y;
    	}
    
    	public int getY() {
    		return y;
    	}
    
    	public List<GameObject> getObjects() {
    		return objects;
    	}
    
    }
    Now, if you have for example woodcutting or mining and you want to replace the rock with an object facing the same direction, you would simply do:
    Code:
    		Region region = Region.loadRegion(tree.getX(), tree.getY());
    		GameObject object = region.getObject(tree.getX(), tree.getY());
    		Client.createObject(tree.getX(), tree.getY(), tree.getDepletedId(), object.getFace(), 10);
    You might want to check for NullPointers or anything, since I don't know if my dump is complete.

    If you are experiencing lag with loading the worldmap ondemand or you have a large amount of ram, you can increase the size of the regions loaded into memory. (It shouldn't be a problem, since it takes less than a ms to load it for me on 1024m and 1.4Ghz)

    PS: this can used for other stuff as well, as no-clip prevention or following.

    If you have any errors, please post.

    This is done for WinterLove, I don't know why.
    Reply With Quote  
     

  2. #2  




    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    27
    Posts
    16,200
    Thanks given
    7,190
    Thanks received
    12,174
    Discord
    View profile
    Rep Power
    5000
    Why not load it through the cache, instead of externally?

    [Only registered and activated users can see links. ]



    Reply With Quote  
     

  3. #3  
    Community Veteran

    WH:II:DOW's Avatar
    Join Date
    Dec 2007
    Age
    32
    Posts
    2,022
    Thanks given
    145
    Thanks received
    871
    Rep Power
    4275
    Quote Originally Posted by Scu11 View Post
    Why not load it through the cache, instead of externally?
    I could have done that if I knew where the map is stored.
    Reply With Quote  
     

  4. #4  




    Scu11's Avatar
    Join Date
    Aug 2007
    Age
    27
    Posts
    16,200
    Thanks given
    7,190
    Thanks received
    12,174
    Discord
    View profile
    Rep Power
    5000
    Quote Originally Posted by WH:II:DOW View Post
    I could have done that if I knew where the map is stored.
    Check hyperion, it does it.

    [Only registered and activated users can see links. ]



    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •