Zone
Zone Class
Zones are named map areas that can be queried for creatures, items, and tiles.
Constructor
Zone(name)| Parameter | Type | Description |
|---|---|---|
name | string | Zone name |
Global Function
Zones() -- Returns table of all zonesMethods
| Method | Returns | Description |
|---|---|---|
getId() | number | Zone ID |
getCreatures() | table | All creatures in zone |
getItems() | table | All items in zone |
getTiles() | table | All tiles in zone |
getGrounds() | table | All ground items |
getCreatureCount() | number | Number of creatures |
getItemCount() | number | Number of items |
getTileCount() | number | Number of tiles |
Examples
Get Zone Info
local function getZoneInfo(zoneName) local zone = Zone(zoneName) if not zone then return nil end
return { id = zone:getId(), creatures = zone:getCreatureCount(), items = zone:getItemCount(), tiles = zone:getTileCount() }endList All Zones
local function listZones() local zones = Zones() for _, zone in ipairs(zones) do print(string.format("Zone %d: %d creatures, %d items, %d tiles", zone:getId(), zone:getCreatureCount(), zone:getItemCount(), zone:getTileCount() )) endendGet Players in Zone
local function getPlayersInZone(zoneName) local zone = Zone(zoneName) if not zone then return {} end
local players = {} for _, creature in ipairs(zone:getCreatures()) do if creature:isPlayer() then table.insert(players, creature:getPlayer()) end end
return playersendZone Event System
-- Broadcast to all players in a zonelocal function zoneAnnounce(zoneName, message) local players = getPlayersInZone(zoneName) for _, player in ipairs(players) do player:sendTextMessage(MESSAGE_STATUS_WARNING, message) endend
-- Check if position is in zonelocal function isInZone(position, zoneName) local tile = Tile(position) if not tile then return false end
return tile:hasZone(Zone(zoneName))endMonster Count in Zone
local function getMonstersInZone(zoneName) local zone = Zone(zoneName) if not zone then return {} end
local monsters = {} for _, creature in ipairs(zone:getCreatures()) do if creature:isMonster() then table.insert(monsters, creature:getMonster()) end end
return monstersend
local function countMonstersByType(zoneName) local monsters = getMonstersInZone(zoneName) local counts = {}
for _, monster in ipairs(monsters) do local name = monster:getName() counts[name] = (counts[name] or 0) + 1 end
return countsendClear Zone Items
local function clearZoneItems(zoneName, itemId) local zone = Zone(zoneName) if not zone then return 0 end
local removed = 0 for _, item in ipairs(zone:getItems()) do if not itemId or item:getId() == itemId then item:remove() removed = removed + 1 end end
return removedendZone-Based Event
local zoneEvent = GlobalEvent("ZoneCheck")
function zoneEvent.onThink(interval) local arenaZone = Zone("pvp_arena") if not arenaZone then return true end
-- Heal all players in arena every minute for _, creature in ipairs(arenaZone:getCreatures()) do if creature:isPlayer() then local player = creature:getPlayer() player:addHealth(player:getMaxHealth() * 0.1) player:addMana(player:getMaxMana() * 0.1) end end
return trueend
zoneEvent:interval(60000)zoneEvent:register()