### Finding Empty Instance Area in RuneScape Map (Java) Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This method searches for an available rectangular space on the game map suitable for creating a new instance. It iterates through potential starting positions, checking if the required number of zones within the specified width and length are empty at the given altitude. The search starts from a specific offset (6400, 0) as required by game scripts. ```java private static final int OFFSET_X = 6400, OFFSET_Z = 0; public Position findEmptyArea(int altitude, int width, int length) { int mapSpanX = width / MapSquare.SIZE; int mapSpanZ = length / MapSquare.SIZE; VerticalSearch: for (int z = OFFSET_Z; z < SIZE; z += length) { HorizontalSearch: for (int x = OFFSET_X; x < SIZE; x += width) { int mapX = x / MapSquare.SIZE; int mapZ = z / MapSquare.SIZE; for (int i = mapX; i < mapX + mapSpanX; i++) { int zoneX = i * Zone.SIZE; int zoneZ = mapZ * Zone.SIZE; Zone zone = zones[altitude][zoneX][zoneZ]; if (zone != null) { continue HorizontalSearch; } } for (int i = mapZ; i < mapZ + mapSpanZ; i++) { int zoneX = mapX * Zone.SIZE; int zoneZ = i * Zone.SIZE; Zone zone = zones[altitude][zoneX][zoneZ]; if (zone != null) { continue VerticalSearch; } } return Position.abs(x, z, altitude); } } return null; } ``` -------------------------------- ### Writing Smart Type Value - Pseudocode Source: https://github.com/runedocs/docs/blob/master/protocol/data-types/index.md This pseudocode illustrates the logic for writing a 'Smart' type value. It checks if the value fits within a byte's maximum range. If it does, it writes the value as a byte; otherwise, it writes it as a short. This optimizes space by using the smallest possible data type. ```text if value < byte max write byte else write short ``` -------------------------------- ### Performing Build Area Rebuild in Java Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This method executes the rebuild process for the game's build area. It calculates the bottom-left zone coordinates based on the new center position and a defined `RADIUS`. It then iterates through altitudes and local zone coordinates to populate the `zones` array with the corresponding zones from the game `World`, updates the `lastRebuild` position, and notifies listeners of the rebuild. ```Java private static final int RADIUS = 6; private void doRebuild(World world, Position position) { int centerZoneX = position.getZoneX(); int centerZoneZ = position.getZoneZ(); int bottomLeftZoneX = centerZoneX - RADIUS; int bottomLeftZoneZ = centerZoneZ - RADIUS; for (int altitude = 0; altitude < ALTITUDE_COUNT; altitude++) { for (int localZoneX = 0; localZoneX < ZONE_COUNT; localZoneX++) { for (int localZoneZ = 0; localZoneZ < ZONE_COUNT; localZoneZ++) { int zoneX = bottomLeftZoneX + localZoneX; int zoneZ = bottomLeftZoneZ + localZoneZ; zones[altitude][localZoneX][localZoneZ] = world.get(altitude, zoneX, zoneZ); } } } lastRebuild = position; notifyRebuild(centerZoneX, centerZoneZ); } ``` -------------------------------- ### Checking Rebuild Requirement in Java Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This snippet provides methods to determine if the player's current position necessitates rebuilding the game's build area. It checks if the player is within a 16-tile margin of the build area's edges using local coordinates calculated by `getX` and `getZ`. The `getX` and `getZ` methods calculate the player's position relative to the bottom-left corner of the last rebuilt area, using the `lastRebuild` position and a defined `RADIUS`. ```Java private boolean rebuildRequired(Position position) { int x = getX(position); int z = getZ(position); boolean reachedLowerEdge = x < 16 || z < 16; boolean reachedUpperEdge = x >= 88 || z >= 88; return reachedLowerEdge || reachedUpperEdge; } public int getX(Position p) { return p.getX() - ((lastRebuild.getZoneX() - RADIUS) * Zone.SIZE); } public int getZ(Position p) { return p.getZ() - ((lastRebuild.getZoneZ() - RADIUS) * Zone.SIZE); } ``` -------------------------------- ### Encoding RuneScape Static Map Rebuild Packet (Java) Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This static method provides an encoder for the `BuildAreaStaticRebuild` network packet, which is used to instruct the game client to load the standard map data for a given area. The encoder writes the center zone coordinates and a list of map square keys required by the client to load the relevant map data. ```java public static EventEncoder encoder() { return (out, evt) -> { out.writeShort(evt.getCenterZoneX()); out.writeShort(evt.getCenterZoneZ()); out.writeShort(evt.getKeySets().size()); for (MapSquareConfig.KeySet keySet : evt.getKeySets()) { for (int key : keySet) { out.writeInt(key); } } }; } ``` -------------------------------- ### Refreshing Build Area Zones in Java Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md These methods handle the visual update of game elements within the build area after a rebuild. `refreshAllZones` iterates through all zones in the current build area and calls `refreshZone` for each. `refreshZone` clears the client's view of the zone's contents and then re-sends data for all floor item stacks and dynamic objects (`Locs`) currently present in that zone. ```Java private void refreshAllZones(Position center) { clearPendingUpdates(); for (int localZoneX = 0; localZoneX < ZONE_COUNT; localZoneX++) { for (int localZoneZ = 0; localZoneZ < ZONE_COUNT; localZoneZ++) { Zone zone = zones[center.getAltitude()][localZoneX][localZoneZ]; if (zone == null) { continue; } refreshZone(zone); } } } private void refreshZone(Zone zone) { clearZone(zone.getPosition()); for (int tileX = 0; tileX < Zone.SIZE; tileX++) { for (int tileZ = 0; tileZ < Zone.SIZE; tileZ++) { FloorItemStack itemStack = zone.getFloorItemStack(tileX, tileZ); if (itemStack == null || itemStack.isEmpty()) { continue; } for (Item item : itemStack) { spawnFloorItem(itemStack.getPosition(), item); } } } Collection dynamicLocs = zone.getLocs(); for (Loc loc : dynamicLocs) { spawnLoc(loc); } } ``` -------------------------------- ### Encoding Dynamic Rebuild Packet in Java Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This Java method provides an `EventEncoder` for the `BuildAreaDynamicRebuild` event. It serializes the event data, including a boolean flag for immediate rebuild, center zone coordinates (X, Z), the size of key sets, and detailed zone information packed into bits. Finally, it writes the integer keys from each key set. ```Java public static EventEncoder encoder() { return (out, evt) -> { out .writeBoolean(evt.requireImmediateRebuild()) .writeShort(evt.getCenterZoneX()) .writeShort(evt.getCenterZoneZ()) .writeShort(evt.getKeySets().size()); bitBlock(BitAccessType.WRITE, out, bitIndex -> { for (int altitude = 0; altitude < evt.getPalette().getHeight(); altitude++) { for (int localZoneX = 0; localZoneX < evt.getPalette().getWidth(); localZoneX++) { for (int localZoneZ = 0; localZoneZ < evt.getPalette().getLength(); localZoneZ++) { BuildAreaPalette.Zone zone = evt.getPalette().get(altitude, localZoneX, localZoneZ); writeBit(out, bitIndex, zone != null); if (zone != null) { writeBits(out, bitIndex, 26, zone.getAltitude() << 24 | zone.getRotation() << 1 | zone.getOriginX() << 14 | zone.getOriginZ() << 3); } } } } }); for (MapSquareConfig.KeySet keySet : evt.getKeySets()) { for (int key : keySet) { out.writeInt(key); } } }; } ``` -------------------------------- ### Copying RuneScape Zone Data (Java) Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This method creates a duplicate of a specified game zone, including its collision matrix which is crucial for player movement and interaction within the zone. It takes the original zone's position and a desired rotation for the copy. The collision flags are copied cell by cell. ```java public Zone copy(Position position, Rotation rotation) { Zone original = Objects.requireNonNull(get(position)); Zone copy = Zone.at(true, position, rotation); for (int x = 0; x < Zone.SIZE; x++) { for (int z = 0; z < Zone.SIZE; z++) { copy.getCollisionMatrix().set(x, z, original.getCollisionMatrix().get(x, z)); } } return copy; } ``` -------------------------------- ### Pasting RuneScape Zone Data (Java) Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This method inserts a previously copied game zone into a specified target position on the map. It first verifies that the target location is currently empty to prevent overwriting existing map data. The zone's internal position is updated to match the target location before being added to the map structure. ```java public void paste(Position target, Zone zone) { if (get(target) != null) { throw new IllegalArgumentException(); } zone.setPosition(Position.abs(target.getZoneX() * Zone.SIZE, target.getZoneZ() * Zone.SIZE, target.getAltitude())); put(zone); } ``` -------------------------------- ### Rotating RuneScape Zone Collision Matrix (Java) Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This method performs a 90-degree clockwise rotation on the zone's internal collision matrix. It creates a new matrix of the same dimensions and copies the flags from the original matrix into the new one with the coordinates transformed for rotation. The internal `flags` array is then replaced with the rotated matrix. ```java public void rotate() { int[][] updated = new int[getWidth()][getLength()]; for (int x = 0; x < getWidth(); ++x) { for (int z = 0; z < getLength(); ++z) { updated[x][z] = flags[getWidth() - z - 1][x]; } } flags = updated; } ``` -------------------------------- ### Defining Game Map Dimensions and Zone Array in Java Source: https://github.com/runedocs/docs/blob/master/functionality/map/index.md This snippet defines constants representing the dimensions of the game map grid, including the total size, the calculated number of zones per axis, and the number of altitude levels. It then declares a 3D array of `Zone` objects to conceptually model the map structure based on these dimensions, illustrating how zones are organized by altitude and coordinates. ```Java private static final int SIZE = 16384, ZONE_COUNT = SIZE / 8, // 16384 tiles on each axis divided by 8 tiles per zone ALTITUDE_COUNT = 4; private final Zone[][][] zones = new Zone[ALTITUDE_COUNT][ZONE_COUNT][ZONE_COUNT]; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.