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.