srunobantana
Member
- Joined
- Oct 24, 2021
- Messages
- 41
- Reaction score
- 18
Note: ChatGPT was used to format and optimize the code, as well as to add prints and comments in both Portuguese (my language) and English (forum language). Apologies for the large code; feel free to delete what you don't need.
About: The player enters the portal and will always return to the point of origin. You can place one portal in each city to teleport the player to the DP Central, Trade Center, or PvP Center. The player will always return to where they came from.
UPDATE: The code has been modified to ensure it returns to the correct position. Comments in both English and Portuguese have been removed for clarity, and print statements have been commented out.
data\scripts\tp_center.lua
LUA:
-- Posições e chaves de armazenamento
local tradeCenterPosition = Position(1787, 1731, 7)
local storageKeyX, storageKeyY, storageKeyZ = 50001, 50002, 50003
local storageKeyDirection = 50004
-- Lista de ActionIDs para os portais de entrada e saída
local entrancePortals = {5051, 5052, 5053}
local exitPortal = 5050
-- Evento para os portais de entrada
local teleports = MoveEvent()
function teleports.onStepIn(player, item, position, fromPosition)
if not player:isPlayer() then return false end
if table.contains(entrancePortals, item:getActionId()) then
-- Armazenar a posição original do jogador
player:setStorageValue(storageKeyX, position.x)
player:setStorageValue(storageKeyY, position.y)
player:setStorageValue(storageKeyZ, position.z)
player:setStorageValue(storageKeyDirection, player:getDirection())
-- Print para verificar as posições armazenadas
-- print("Armazenando a posição original do jogador: X="..position.x..", Y="..position.y..", Z="..position.z)
-- Teleporta o jogador para o Trade Center
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
player:teleportTo(tradeCenterPosition)
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Você foi transportado para o DP Center.")
return true
end
return false
end
for _, aid in ipairs(entrancePortals) do
teleports:aid(aid)
end
teleports:type("stepin")
teleports:register()
-- Evento para os portais de saída
local returnPortal = MoveEvent()
function returnPortal.onStepIn(player, item, position, fromPosition)
if not player:isPlayer() then return false end
if item:getActionId() == exitPortal then
local x = player:getStorageValue(storageKeyX)
local y = player:getStorageValue(storageKeyY)
local z = player:getStorageValue(storageKeyZ)
local direction = player:getStorageValue(storageKeyDirection)
-- Print para verificar os valores de retorno
-- print("Valores de retorno: X="..x..", Y="..y..", Z="..z..", Direção="..direction)
if x > 0 and y > 0 and z >= 0 then
local returnPosition = Position(x, y, z)
local newReturnPosition = Position(x, y, z)
-- Ajustar a posição com base na direção do jogador ao entrar
if direction == NORTH then
newReturnPosition.y = newReturnPosition.y + 1
elseif direction == SOUTH then
newReturnPosition.y = newReturnPosition.y - 1
elseif direction == EAST then
newReturnPosition.x = newReturnPosition.x - 1
elseif direction == WEST then
newReturnPosition.x = newReturnPosition.x + 1
end
-- Print para verificar a nova posição de retorno
-- print("Nova posição de retorno: X="..newReturnPosition.x..", Y="..newReturnPosition.y..", Z="..newReturnPosition.z)
-- Teleporta o jogador para a nova posição
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
player:teleportTo(newReturnPosition)
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Você retornou ao portal de origem.")
else
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Erro: Posição de retorno não encontrada.")
end
end
return true
end
returnPortal:aid(exitPortal)
returnPortal:type("stepin")
returnPortal:register()
Last edited: