Class Coord

java.lang.Object
org.rspeer.game.coord.Coord
All Implemented Interfaces:
SceneNode

public class Coord extends Object implements SceneNode
Represents a world coordinate on the game map. Also referred to as Tile, or Position which Coord is here to replace.
  • Field Details

    • NIL

      public static final Coord NIL
  • Method Details

    • from

      public static Coord from(int x, int y, int floorLevel, float subX, float subY)
    • from

      public static Coord from(int x, int y, int floorLevel)
      Creates a new Coord from world-space X/Y coordinates and a floor level.

      This is the primary factory method for constructing world coordinates.

      Parameters:
      x - the world X coordinate
      y - the world Y coordinate
      floorLevel - the floor/plane (typically 0–3)
      Returns:
      a new Coord representing the given world position
    • from

      public static Coord from(int x, int y)
      Creates a new Coord at the specified world X/Y coordinates on floor level 0.

      This is a convenience overload for situations where the floor is implicitly ground level.

      Parameters:
      x - the world X coordinate
      y - the world Y coordinate
      Returns:
      a new Coord at the given world position on floor 0
    • fromGrid

      public static Coord fromGrid(jag.oldscape.RSWorldMatrix matrix, CoordGrid grid)
      Converts a CoordGrid (matrix-local tile coordinate) into a world-space Coord using the given RSWorldMatrix.

      In the engine, a CoordGrid stores coordinates relative to the originating matrix/scene. To convert these to absolute world coordinates, the matrix base is applied:

      worldX = grid.x + matrix.getBaseX()
      worldY = grid.y + matrix.getBaseY()
      

      The floor level is preserved.

      Parameters:
      matrix - the world matrix providing base coordinate offsets; must not be null
      grid - the matrix-local tile coordinate to convert
      Returns:
      the corresponding world-space Coord
    • fromGrid

      public static Coord fromGrid(int matrixId, CoordGrid grid)
      Converts a CoordGrid into a world-space Coord by resolving the associated RSWorldMatrix using the given matrix ID.

      This is equivalent to:

      Coord.fromGrid(Game.getMatrix(matrixId), grid)
      
      Parameters:
      matrixId - the matrix ID used to look up the RSWorldMatrix
      grid - the matrix-local tile coordinate to convert
      Returns:
      the world-space Coord, or Coord.NIL if the matrix cannot be resolved
    • fromGrid

      public static Coord fromGrid(CoordGrid grid)
      Converts a CoordGrid into a world-space Coord using the matrix referenced directly by the grid's own matrixId.

      This is the most convenient overload and is equivalent to:

      Coord.fromGrid(Game.getMatrix(grid.getMatrixId()), grid)
      
      Parameters:
      grid - the matrix-local tile coordinate to convert
      Returns:
      the corresponding world-space Coord, or Coord.NIL if the grid's matrix cannot be resolved
    • fromFine

      public static Coord fromFine(jag.oldscape.RSWorldMatrix matrix, CoordFine fine)
      Converts a CoordFine (fine-coordinate space, usually 128 sub-units per tile) into a world-space Coord, preserving sub-tile precision and automatically resolving instance and subworld transformations.

      This method performs the following steps in order:

      1. Instance coordinate translation:
        If the fine coordinate originates inside a dynamically-built instanced region, the underlying instance template chunk is resolved and converted into its corresponding top-level world tile via fromInstance(...).
      2. Subworld → root world transformation:
        If the provided RSWorldMatrix does not match the game's top-level world matrix, the coordinate is transformed using the appropriate baseX/baseY offset via fromMatrix(...).
      3. Fine → world tile conversion:
        The fine-coordinate X/Y values are divided by CoordFine.UNITS_PER_SQUARE to recover the integer grid coordinate, and the remainder is used to compute subX/subY, which describe the precise fractional position within the tile (0.0–1.0).

      The returned Coord always describes the final, fully-resolved world position as seen by pathfinding, collision, and interaction systems. If an instance/subworld transformation is required, the intermediate transformed Coord retains a reference to its pre-transformed world-space source coordinate via getSource().

      Parameters:
      matrix - the world matrix used to interpret the fine coordinate's local base
      fine - the fine-resolution coordinate to convert
      Returns:
      a fully resolved, world-space Coord with sub-tile precision
    • fromFine

      public static Coord fromFine(CoordFine fine)
      Converts a CoordFine into a world Coord, resolving the associated RSWorldMatrix via Game.getMatrix(int).

      This is a convenience overload that calls fromFine(RSWorldMatrix, CoordFine) with the matrix referenced by fine.getMatrixId().

      Parameters:
      fine - the fine-coordinate to convert; must not be null
      Returns:
      a Coord representing the world coord derived from the fine position
    • fromPacked

      public static Coord fromPacked(int packed)
      Unpacks a 32-bit packed coordinate into a Coord.

      The packed format uses:

      • bits 28–29: floor level (0–3)
      • bits 14–27: X coordinate (0–16383)
      • bits 0–13 : Y coordinate (0–16383)
      Parameters:
      packed - the packed coordinate value
      Returns:
      a Coord extracted from the packed integer
    • rotate

      public static Coord rotate(Coord coord, int rotation)
      Rotates a coordinate within its containing 8x8 chunk by the given rotation.

      Rotation is performed around the origin of the 8x8 chunk containing coord, using the standard RuneScape convention:

      • 0 – no rotation
      • 1 – 90 degrees clockwise
      • 2 – 180 degrees
      • 3 – 270 degrees clockwise
      The floor level is preserved.
      Parameters:
      coord - the coordinate to rotate
      rotation - rotation value in the range 0–3
      Returns:
      a new Coord representing the rotated position
    • pack

      public static int pack(int x, int y, int floor)
      Packs a tile coordinate into a 32-bit integer using the standard RuneScape-style coordinate packing format.

      The layout is:

       floor (2 bits)  |   x (14 bits)   |   y (14 bits)
          [28..29]     |     [14..27]    |     [0..13]
      
      Parameters:
      x - the X tile coordinate (0–16383)
      y - the Y tile coordinate (0–16383)
      floor - the floor level (0–3)
      Returns:
      a packed 32-bit integer containing x, y, and floor
    • getMatrixId

      public int getMatrixId()
      Specified by:
      getMatrixId in interface SceneNode
    • getX

      public int getX()
      Specified by:
      getX in interface SceneNode
    • getY

      public int getY()
      Specified by:
      getY in interface SceneNode
    • getPosition

      public Position getPosition()
      Specified by:
      getPosition in interface SceneNode
    • getCoord

      public Coord getCoord()
      Specified by:
      getCoord in interface SceneNode
    • getCoordFine

      public CoordFine getCoordFine()
      Specified by:
      getCoordFine in interface SceneNode
    • getCoordView

      public CoordView getCoordView()
      Specified by:
      getCoordView in interface SceneNode
    • getFloorLevel

      public int getFloorLevel()
      Specified by:
      getFloorLevel in interface SceneNode
    • getSource

      public Coord getSource()
      Returns the underlying world-space coordinate that this Coord was derived from during instance or subworld transformation.

      If this coordinate was produced by fromFine(RSWorldMatrix, CoordFine) and required instance template translation or subworld-to-root-world matrix adjustment, the returned value corresponds to the original resolved world coordinate.

      If no transformation occurred, this method simply returns this.

      In short:

      • Normal world coords: returns this
      • Instanced coords → world coords: returns the raw instance coord
      • Subworld coords → top-level coords: returns the raw subworld coord
      Returns:
      the world-space source coordinate, or this coordinate if none exists
    • getSubX

      public float getSubX()
    • getSubY

      public float getSubY()
    • getPreciseX

      public float getPreciseX()
    • getPreciseY

      public float getPreciseY()
    • getRegionId

      public int getRegionId()
      Returns the region ID for this coordinate.

      The region ID is computed from the 8x8 regions:

      regionX = x >> 6
      regionY = y >> 6
      regionId = (regionX invalid input: '<'invalid input: '<' 8) | regionY
      
      Returns:
      the packed region ID
    • translate

      public Coord translate(Direction direction)
    • translate

      public Coord translate(int x, int y)
    • dx

      public Coord dx(int x)
    • dy

      public Coord dy(int y)
    • getInstanced

      public List<Coord> getInstanced()
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object