api_version = "1.12.0.0"
warp_count={}
prev_warp_count={}
prev_x={}
prev_y={}
prev_z={}
function OnScriptLoad()
register_callback(cb["EVENT_WARP"],"OnWarp")
register_callback(cb['EVENT_JOIN'], "OnPlayerConnect")
register_callback(cb['EVENT_LEAVE'], "OnPlayerLeave")
register_callback(cb["EVENT_TICK"],"OnTick")
end
function OnPlayerConnect(PlayerIndex)
pi=tonumber(PlayerIndex)
--assuming 'antiwarp 3' in the sapp init.txt
warp_count[pi]=3
end
function OnPlayerLeave(PlayerIndex)
pi=tonumber(PlayerIndex)
--reset the counter for players that leave
warp_count[pi]=0
end
function OnTick()
for i=1,16,1 do
if (player_alive(i)) then
local player = get_dynamic_player(i)
vehicle=read_dword(player + 0x11C)
if (vehicle == 0xFFFFFFFF) then --only do warp distance calculation if player is not in a vehicle
p_x = read_float(player + 0x5C)
p_y = read_float(player + 0x60)
p_z = read_float(player + 0x5C+8)
--cprint(p_x..','..p_y)
if (prev_warp_count[i]~=warp_count[i]) then
if (prev_x[i]~=nil and prev_y[i]~=nil and prev_z[i]~=nil) then
--pushing a player with a vehicle can trigger EVENT_WARP even if both players have a perfect connection
warp_distance=math.sqrt(((p_x-prev_x[i])*(p_x-prev_x[i]))+((p_y-prev_y[i])*(p_y-prev_y[i]))+((p_z-prev_z[i])*(p_z-prev_z[i])))
--warp_distance will be influenced if a player is being pushed by a vehicle, falling, walking up a ramp and possibly other things
m_player=get_player(i)
player_speed=read_float(m_player + 0x6C) --warp distance is influenced by player speed while walking
cprint(i..' warp '..warp_distance..' '..player_speed)
end
end
end
if (prev_x[i]~=nil and prev_y[i]~=nil and prev_z[i]~=nil) then
distance=math.sqrt(((p_x-prev_x[i])*(p_x-prev_x[i]))+((p_y-prev_y[i])*(p_y-prev_y[i]))+((p_z-prev_z[i])*(p_z-prev_z[i])))
--cprint(distance) --print distance moved each tick
end
prev_x[i]=p_x
prev_y[i]=p_y
prev_z[i]=p_z
prev_warp_count[i]=warp_count[i]
end
end
end
function OnWarp(PlayerIndex)
--only fires when a player is moving or being moved
--there seems to be a cooldown effect where if a player stands still for a while, they must warp 2 or 3 more times in order for OnWarp to fire again
pi=tonumber(PlayerIndex)
s=get_var(PlayerIndex,"$name").." warped "..warp_count[pi]
cprint(s)
--say_all(s)
warp_count[pi]=warp_count[pi]+1
end