Tile Type
Tile
A tile represents a specific location in the game world with x, y coordinates and a floor level.
Properties
| Property | Type | Description |
|---|---|---|
| x | number | The x coordinate of the tile |
| y | number | The y coordinate of the tile |
| floor | number | The floor/height level of the tile |
Constructor
Tile.new
Tile.new(x: number, y: number, floor?: number) -> Tile
Tile.new(tbl: {x: number, y: number, floor?: number}) -> Tile
Creates a new Tile instance. Accepts either individual coordinates or a table with coordinate fields.
Parameters (coordinate form):
x(number): The x coordinate of the tiley(number): The y coordinate of the tilefloor(number, optional): The floor/height level of the tile (defaults to 0)
Parameters (table form):
tbl(table): A table withx,y, and optionallyfloorfields
Returns:
Tile: A new Tile instance
Examples:
-- Using coordinates
local tile1 = Tile.new(3200, 3200, 0)
local tile2 = Tile.new(3200, 3200) -- floor defaults to 0
-- Using a table
local tile3 = Tile.new({x = 3200, y = 3200, floor = 0})
local tile4 = Tile.new({x = 3200, y = 3200}) -- floor defaults to 0
-- Useful when working with coordinate data from other sources
local coords = {x = 3200, y = 3200, floor = 0}
local tile = Tile.new(coords)
Methods
distance
tile:distance() -> number
Calculates the Euclidean distance from this tile to the local player's tile. Returns a very large number if there is no local player available.
Parameters:
- None
Returns:
number: The distance to the local player, or a very large number if no player is available
Example:
local target_tile = Tile.new(3200, 3200, 0)
local distance = target_tile:distance()
if distance < 10 then
logger:info("Target is close to player!")
else
logger:info("Target is " .. distance .. " tiles away")
end
distance_to
tile:distance_to(other: Tile) -> number
Calculates the Euclidean distance to another tile.
Parameters:
other(Tile): The target tile
Returns:
number: The distance between the tiles
Example:
local tile1 = Tile.new(3200, 3200, 0)
local tile2 = Tile.new(3210, 3210, 0)
local distance = tile1:distance_to(tile2)
logger:info("Distance: " .. distance)
manhattan_distance_to
tile:manhattan_distance_to(other: Tile) -> number
Calculates the Manhattan distance (taxicab distance) to another tile. This is the sum of absolute differences in x and y coordinates.
Parameters:
other(Tile): The target tile
Returns:
number: The Manhattan distance between the tiles
Example:
local tile1 = Tile.new(3200, 3200, 0)
local tile2 = Tile.new(3210, 3210, 0)
local distance = tile1:manhattan_distance_to(tile2)
-- Returns 20 (10 + 10)
is_within_distance
tile:is_within_distance(other: Tile, distance: number) -> boolean
Checks if another tile is within a specified distance.
Parameters:
other(Tile): The tile to check distance todistance(number): The maximum distance
Returns:
boolean: True if the other tile is within the specified distance
Example:
local player_tile = players:local_player():tile()
local target_tile = Tile.new(3200, 3200, 0)
if player_tile:is_within_distance(target_tile, 10) then
logger:info("Target is nearby!")
end
is_same_floor
tile:is_same_floor(other: Tile) -> boolean
Checks if another tile is on the same floor level.
Parameters:
other(Tile): The tile to compare floor levels with
Returns:
boolean: True if both tiles are on the same floor
Example:
local tile1 = Tile.new(3200, 3200, 0)
local tile2 = Tile.new(3210, 3210, 0)
if tile1:is_same_floor(tile2) then
logger:info("Same floor!")
end
derive
tile:derive(dx: number, dy: number) -> Tile
Creates a new tile offset by the specified x and y values from this tile.
Parameters:
dx(number): The x offset to adddy(number): The y offset to add
Returns:
Tile: A new tile at the offset position
Example:
local base_tile = Tile.new(3200, 3200, 0)
local offset_tile = base_tile:derive(5, -3)
-- offset_tile is at (3205, 3197, 0)
-- Useful for creating patrol patterns
local patrol_points = {
base_tile,
base_tile:derive(10, 0),
base_tile:derive(10, 10),
base_tile:derive(0, 10)
}
reachable
tile:reachable() -> boolean
Check if tile is reachable from the local player's position using pathfinding.
Returns:
boolean: True if the tile can be reached from the player's current position, false otherwise
Example:
local destination = Tile.new(3200, 3200, 0)
if destination:reachable() then
logger:info("Can walk to this tile!")
-- Perform pathfinding/walking
else
logger:warn("Tile is blocked or unreachable")
end
move_mouse_to()
tile:move_mouse_to() -> boolean
Move the mouse to this tile with human-like movement.
Returns:
boolean: True on success, false if the position is invalid or the move fails
Example:
local tile = Tile.new(3200, 3200, 0)
if tile:move_mouse_to() then
mouse:click()
end
track()
tile:track() -> ()
Start tracking this tile: the mouse follows the tile as its screen position changes until another mouse action is performed or you call mouse:stop_track().
Example:
local tile = Tile.new(3200, 3200, 0)
tile:track()
sleep(1500)
mouse:stop_track()
Common Patterns
Distance Checking
local player_tile = players:local_player():tile()
local npc = npcs:find_first({name = "Guard"})
if npc then
local npc_tile = npc:tile()
local distance = player_tile:distance_to(npc_tile)
if distance < 5 then
logger:info("Guard is close!")
end
end
Area Navigation
local current = players:local_player():tile()
local destination = Tile.new(3200, 3200, 0)
while current:distance_to(destination) > 2 do
-- Walk towards destination
sleep(600)
current = players:local_player():tile()
end
Creating Offset Positions
local center = Tile.new(3200, 3200, 0)
-- Create a square around the center
local surrounding_tiles = {
center:derive(-1, -1),
center:derive(0, -1),
center:derive(1, -1),
center:derive(-1, 0),
center:derive(1, 0),
center:derive(-1, 1),
center:derive(0, 1),
center:derive(1, 1)
}
Floor-Aware Positioning
local player_tile = players:local_player():tile()
local objects = game_objects:find({name = "Ladder"})
for _, obj in ipairs(objects) do
local obj_tile = obj:tile()
-- Only interact with ladders on the same floor
if obj_tile:is_same_floor(player_tile) then
if obj_tile:is_within_distance(player_tile, 3) then
obj:interact("Climb-up")
break
end
end
end
See Also
- Point - 2D screen coordinates
- Area - Rectangular regions of tiles
- Player - Has tile() method
- NPC - Has tile() method
- GameObject - Has tile() method