local positionData = {}
local config = {
actionids = {45001, 45002, 45003}, -- {entryTile, playerTile, exitTile}
trainingPartner = "Training Monk",
exitPosition = false, -- use Position(x, y, z) OR false -- false will teleport player to their home town.
boundaries = {{x = 1015, y = 1015}, {x = 1515, y = 1515}, z = 7}, -- {{north_west_corner}, {south_east_corner}, floor_level}
portalID = 1387
}
local moveEvent = MoveEvent()
moveEvent:type("stepin")
function moveEvent.onStepIn(player, item, position, fromPosition)
if not player:isPlayer() then
return true
end
local actionID = item:getActionId()
-- if teleporting out of trainers
if actionID == config.actionids[3] then
-- teleport player out
local exit_position = config.exitPosition
if exit_position == false then
exit_position = player:getTown():getTemplePosition()
end
player:teleportTo(exit_position)
-- remove creatures
local spec = Game.getSpectators(fromPosition, false, false, 3, 3, 3, 2)
for i = 1, #spec do
spec[i]:getPosition():sendMagicEffect(CONST_ME_POFF)
spec[i]:remove()
end
return true
-- if teleporting into trainers
elseif actionID == config.actionids[1] then
for i = 1, #positionData do
local spec, players = Game.getSpectators(positionData[i][1], false, false, 2, 2, 2, 2), 0
-- if creatures exist in training position, check if any are a player
if #spec > 0 then
for i = 1, #spec do
if spec[i]:isPlayer() then
if spec[i]:getIp() ~= 0 then -- if player is still online (not x-logged or disconnected)
players = 1
end
break
end
end
end
-- if no online player found, and creatures exist, remove all creatures in training room.
if players == 0 and #spec > 0 then
for i = 1, #spec do
spec[i]:remove()
end
spec = {} -- clear spectator table
end
-- if empty, teleport in and create training creatures
if #spec < 1 then
player:teleportTo(positionData[i][1])
for n = 1, 3 do
Game.createMonster(config.trainingPartner, positionData[i][n + 1])
end
return true
end
end
end
-- no locations available to teleport to
local text = "Sorry, no training tiles are available. Try again later."
if not positionData[1] then
text = "Training tiles are still being populated by the server. Please wait 10 seconds."
end
player:sendTextMessage(MESSAGE_STATUS_WARNING, text)
player:teleportTo(fromPosition, true)
return true
end
moveEvent:aid(config.actionids[1], config.actionids[3])
moveEvent:register()
local direction = {
{-1, 0, -1, 1, -1, -1, -3, 0}, -- west
{1, 0, 1, 1, 1, -1, 3, 0}, -- east
{0, -1, 1, -1, -1, -1, 0, -3}, -- north
{0, 1, 1, 1, -1, 1, 0, 3} -- south
}
local function collect_positionData(current_x, current_y)
local position = Position(current_x, current_y, config.boundaries.z)
local tile = Tile(position)
-- if tile exists
if tile then
-- if tile has actionid
local item = Item(tile:getGround().uid)
if item:getActionId() == config.actionids[2] then
-- find positions to spawn training creatures
for i = 1, 4 do -- 4 possible directions
-- make sure tile exists
local check_tile = Tile(Position(current_x + direction[i][1], current_y + direction[i][2], config.boundaries.z))
if check_tile then
-- if teleport exists on tile
local teleport_count = check_tile:getItemCountById(config.portalID)
if teleport_count > 0 then
-- we know which direction to spawn training creatures
local pos_1 = Position(current_x + direction[i][3], current_y + direction[i][4], config.boundaries.z)
local pos_2 = Position(current_x + direction[i][5], current_y + direction[i][6], config.boundaries.z)
local pos_3 = Position(current_x + direction[i][7], current_y + direction[i][8], config.boundaries.z)
-- add positions into table
positionData[#positionData + 1] = {position, pos_1, pos_2, pos_3}
break
end
end
end
end
end
-- calculate next tile
if current_x == config.boundaries[2].x then
current_x = config.boundaries[1].x
current_y = current_y + 1
else
current_x = current_x + 1
end
-- check if next tile is within check area
if config.boundaries[2].y + 1 ~= current_y then
addEvent(collect_positionData, 1, current_x, current_y)
end
end
local globalEvent = GlobalEvent("training_Monks_One")
globalEvent:type("startup")
function globalEvent.onStartup()
addEvent(collect_positionData, 1000, config.boundaries[1].x, config.boundaries[1].y)
return true
end
globalEvent:register()
local loginEvent = CreatureEvent("training_Monks_One")
loginEvent:type("login")
function loginEvent.onLogin(player)
local north_west = Position(config.boundaries[1].x, config.boundaries[1].y, config.boundaries.z)
local south_east = Position(config.boundaries[2].x, config.boundaries[2].y, config.boundaries.z)
if player:getPosition():isInRange(north_west, south_east) then
local exit_position = config.exitPosition
if exit_position == false then
exit_position = player:getTown():getTemplePosition()
end
player:teleportTo(exit_position)
end
return true
end
loginEvent:register()