Area Type
Area
An Area represents a region in the game world. It can be either:
- Rectangular: Defined by two corner tiles (southwest and northeast)
- Polygon: Defined by 3 or more vertex tiles forming an arbitrary shape
Properties
| Property | Type | Description |
|---|---|---|
| sw_corner | Tile? | The southwest corner (rectangles only, nil for polygons) |
| ne_corner | Tile? | The northeast corner (rectangles only, nil for polygons) |
| floor | number | The floor level of the area |
| is_polygon | boolean | True if this is a polygon area |
Constructors
Area.new
Area.new(corner1: Tile, corner2: Tile) -> Area
Creates a rectangular area from two corner tiles.
Parameters:
corner1(Tile): First corner tilecorner2(Tile): Second corner tile (corners are automatically normalized)
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
Area.from_nw_to_se
Area.from_nw_to_se(nw: Tile, se: Tile) -> Area
Creates a rectangular area with explicit northwest to southeast corners.
Parameters:
nw(Tile): Northwest corner tilese(Tile): Southeast corner tile
Example:
local area = Area.from_nw_to_se(
Tile.new(3200, 3210, 0), -- NW corner (higher y)
Tile.new(3210, 3200, 0) -- SE corner (lower y)
)
Area.from_center
Area.from_center(center: Tile, radius: number) -> Area
Creates a rectangular area centered on a tile with a given radius.
Parameters:
center(Tile): The center tileradius(number): Distance from center to edges
Example:
local center = Tile.new(3205, 3205, 0)
local area = Area.from_center(center, 5)
-- Creates 11x11 area from (3200, 3200) to (3210, 3210)
Area.from_tiles
Area.from_tiles(tile1: Tile, tile2: Tile, ...) -> Area
Creates an area from multiple tiles. This is the recommended way to create areas.
Parameters:
- 2 tiles: Creates a rectangular area
- 3+ tiles: Creates a polygon area with the tiles as vertices
Example (Rectangle):
local rect = Area.from_tiles(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
Example (Polygon):
-- Create an irregular polygon area
local poly = Area.from_tiles(
Tile.new(3122, 3624, 0),
Tile.new(3121, 3632, 0),
Tile.new(3134, 3636, 0),
Tile.new(3140, 3626, 0),
Tile.new(3130, 3621, 0)
)
-- Check if player is inside the polygon
local player_tile = players:local_player():tile()
if poly:contains_tile(player_tile) then
logger:info("Player is inside the polygon!")
end
Note: All tiles in a polygon must be on the same floor level.
Methods
contains_tile
area:contains_tile(tile: Tile) -> boolean
Checks if a tile is within this area. Works for both rectangles and polygons.
Parameters:
tile(Tile): The tile to check
Returns:
boolean: True if the tile is within the area
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local player_tile = players:local_player():tile()
if area:contains_tile(player_tile) then
logger:info("Player is in the area!")
end
center
area:center() -> Tile?
Returns the center tile of the area.
- For rectangles: Returns the geometric center
- For polygons: Returns the centroid (average of all vertices)
Returns:
Tile?: The center tile, or nil if the area is empty
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local center = area:center()
logger:info("Center: " .. center.x .. ", " .. center.y)
width
area:width() -> number?
Returns the width of the area (rectangles only).
Returns:
number?: The width in tiles, or nil for polygons
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local w = area:width() -- Returns 11
height
area:height() -> number?
Returns the height of the area (rectangles only).
Returns:
number?: The height in tiles, or nil for polygons
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local h = area:height() -- Returns 11
size
area:size() -> number?
Returns the total number of tiles in the area (rectangles only).
Returns:
number?: The total tile count (width x height), or nil for polygons
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local total = area:size() -- Returns 121 (11 x 11)
overlaps
area:overlaps(other: Area) -> boolean
Checks if this area overlaps with another area (rectangles only).
Parameters:
other(Area): The other area to check
Returns:
boolean: True if the areas overlap (always false for polygons)
Example:
local area1 = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local area2 = Area.new(
Tile.new(3205, 3205, 0),
Tile.new(3215, 3215, 0)
)
if area1:overlaps(area2) then
logger:info("Areas overlap!")
end
tiles
area:tiles() -> table
Returns tiles associated with the area.
- For rectangles: Returns all tiles within the area
- For polygons: Returns the vertex tiles
Returns:
table: Array of Tile objects
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3202, 3202, 0)
)
local all_tiles = area:tiles()
for _, tile in ipairs(all_tiles) do
logger:info("Tile: " .. tile.x .. ", " .. tile.y)
end
random_tile
area:random_tile() -> Tile?
Returns a random tile within the area.
- For rectangles: Returns any tile within bounds
- For polygons: Uses rejection sampling to find a point inside the polygon
Returns:
Tile?: A randomly selected tile
Example:
local area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local random = area:random_tile()
logger:info("Random tile: " .. random.x .. ", " .. random.y)
Common Patterns
Defining Safe Zones
local safe_zone = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local player_tile = players:local_player():tile()
if not safe_zone:contains_tile(player_tile) then
logger:warn("Player outside safe zone!")
end
Polygon Areas for Irregular Shapes
-- Define a custom-shaped area (e.g., an L-shaped room, irregular terrain)
local custom_area = Area.from_tiles(
Tile.new(3100, 3100, 0),
Tile.new(3100, 3110, 0),
Tile.new(3105, 3110, 0),
Tile.new(3105, 3105, 0),
Tile.new(3110, 3105, 0),
Tile.new(3110, 3100, 0)
)
if custom_area:contains_tile(player_tile) then
logger:info("Player is in the custom area")
end
Multi-Floor Areas
-- Areas on different floors
local ground_floor = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3210, 3210, 0)
)
local first_floor = Area.new(
Tile.new(3200, 3200, 1),
Tile.new(3210, 3210, 1)
)
local player_tile = players:local_player():tile()
if ground_floor:contains_tile(player_tile) then
logger:info("On ground floor")
elseif first_floor:contains_tile(player_tile) then
logger:info("On first floor")
end
Resource Gathering in an Area
local mining_area = Area.new(
Tile.new(3280, 3360, 0),
Tile.new(3290, 3370, 0)
)
local rocks = game_objects:find({
name = "Copper ore",
within_area = mining_area
})
for _, rock in ipairs(rocks) do
rock:interact("Mine")
sleep(1000)
end
Random Patrol Within Area
local patrol_area = Area.new(
Tile.new(3200, 3200, 0),
Tile.new(3220, 3220, 0)
)
while script_running() do
local destination = patrol_area:random_tile()
logger:info("Walking to: " .. destination.x .. ", " .. destination.y)
-- movement:walk_to(destination)
sleep(5000)
end
Area Around Player
local function area_around_player(radius)
local player = players:local_player()
if not player then return nil end
return Area.from_center(player:tile(), radius)
end
local nearby = area_around_player(10)
See Also
- Tile - Individual tile positions
- Rectangle - Screen-space rectangles
- GameObject - Can be filtered by area
- NPC - Can be filtered by area