Skip to content

Instantly share code, notes, and snippets.

@Zalgo2462
Created August 24, 2011 04:06
Show Gist options
  • Save Zalgo2462/1167285 to your computer and use it in GitHub Desktop.
Save Zalgo2462/1167285 to your computer and use it in GitHub Desktop.
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.internal.event.PaintListener;
import org.rsbot.script.methods.*;
import org.rsbot.script.wrappers.Area;
import org.rsbot.script.wrappers.Tile;
import java.awt.*;
import java.util.*;
@ScriptManifest(name = "Room Generator", authors = {"Zalgo"}, description = "Builds a list of rooms from collision flags")
public class RoomGeneration extends Script implements PaintListener {
private ArrayList<Room> rooms = null;
public enum Flag {
// WALL_NORTHWEST(0x1),
// WALL_NORTH(0x2),
// WALL_NORTHEAST(0x4),
// WALL_EAST(0x8),
// WALL_SOUTHEAST(0x10),
// WALL_SOUTH(0x20),
// WALL_SOUTHWEST(0x40),
// WALL_WEST(0x80),
// OBJECT_TILE(0x100),
WALL_BLOCK_NORTHWEST(0x200),
WALL_BLOCK_NORTH(0x400),
WALL_BLOCK_NORTHEAST(0x800),
WALL_BLOCK_EAST(0x1000),
WALL_BLOCK_SOUTHEAST(0x2000),
WALL_BLOCK_SOUTH(0x4000),
WALL_BLOCK_SOUTHWEST(0x8000),
WALL_BLOCK_WEST(0x10000),
OBJECT_BLOCK(0x20000),
DECORATION_BLOCK(0x40000),
WALL_ALLOW_RANGE_NORTHWEST(0x400000),
WALL_ALLOW_RANGE_NORTH(0x800000),
WALL_ALLOW_RANGE_NORTHEAST(0x1000000),
WALL_ALLOW_RANGE_EAST(0x2000000),
WALL_ALLOW_RANGE_SOUTHEAST(0x4000000),
WALL_ALLOW_RANGE_SOUTH(0x8000000),
WALL_ALLOW_RANGE_SOUTHWEST(0x10000000),
WALL_ALLOW_RANGE_WEST(0x20000000),
OBJECT_ALLOW_RANGE(0x40000000),
WATER(0x1280100),
NULL(0);
private int flag;
Flag(int flag){
this.flag = flag;
}
public int getFlag() {
return this.flag;
}
public String toString() {
return this.name();
}
}
@Override
protected int loop() {
rooms = generateRooms(Flag.NULL);
return 10000;
}
public void onRepaint(Graphics graphics) {
try {
if(rooms != null) {
for(Room room : rooms) {
room.draw(graphics, Color.RED);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private Flag getFlag(Tile tile) {
Tile offset = Walking.getCollisionOffset(Game.getPlane());
int[][] flags = Walking.getCollisionFlags(Game.getPlane());
int tileFlag = flags[tile.getX() - (Game.getBaseX() + offset.getX())][tile.getY() - (Game.getBaseY() + offset.getY())];
for(int iii = 0; iii < Flag.values().length; iii++) {
int flag = Flag.values()[iii].getFlag();
if((flag & tileFlag) != 0) {
return Flag.values()[iii];
}
}
return Flag.NULL;
}
private Tile[] getTilesWithFlags(Flag... flags) {
Tile[] loaded = getLoadedArea();
ArrayList<Tile> filtered = new ArrayList<Tile>();
for(Tile toCheck : loaded) {
for(Flag flag : flags) {
if(getFlag(toCheck) == flag){
filtered.add(toCheck);
break;
}
}
}
return filtered.toArray(new Tile[filtered.size()]);
}
private Tile[] getLoadedArea() {
Tile base = Game.getMapBase();
ArrayList<Tile> tiles = new ArrayList<Tile>();
tiles.add(base);
int index = 1;
for(int x = 1; x < 105; x++) {
for(int y = 1; y < 104; y++) {
tiles.add(index, base.derive(x - 1, y));
index++;
}
if(x != 104){
tiles.add(index, base.derive(x, 0));
index++;
}
}
return tiles.toArray(new Tile[tiles.size()]);
}
private ArrayList<Room> generateRooms(Flag... flags) {
//BFS SEARCH
ArrayList<Tile> unchecked = new ArrayList<Tile>();
for(Flag flag : flags) {
unchecked.addAll(Arrays.asList(getTilesWithFlags(flag)));
}
if(unchecked.size() != 0) {
ArrayList<Room> rooms = new ArrayList<Room>();
while(!unchecked.isEmpty()){
ArrayList<Tile> areaTiles = new ArrayList<Tile>();
Queue<Tile> queue = new LinkedList<Tile>();
areaTiles.add(unchecked.get(0));
queue.add(unchecked.get(0));
unchecked.remove(0);
while(!queue.isEmpty()) {
Tile t = queue.remove();
Tile child = null;
while((child = getUncheckedChild(t, unchecked)) != null) {
areaTiles.add(child);
queue.add(child);
unchecked.remove(child);
}
}
rooms.add(new Room(areaTiles.toArray(new Tile[areaTiles.size()])));
}
return rooms;
}
return null;
}
private Tile getUncheckedChild(Tile parent, ArrayList<Tile> unchecked) {
Tile[] children = new Tile[] {parent.derive(1, 0), parent.derive(0, 1),
parent.derive(-1, 0), parent.derive(0, -1)};
for(Tile tile : children) {
if(unchecked.contains(tile))
return tile;
}
return null;
}
private class Room {
private Tile[] tiles;
public Room(Tile[] tiles) {
this.tiles = tiles;
}
public boolean contains(Tile inTile) {
for(Tile tile : tiles) {
if(tile.equals(inTile))
return true;
}
return false;
}
public void draw(Graphics graphics, Color color) {
for(Tile tile : tiles) {
if(tile.isOnMap())
tile.draw(graphics, color, 0);
}
}
public Area getArea() {
return new Area(tiles);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment