sampev = require("lib.samp.events")
vec = require("vector3d")
font = renderCreateFont("MENU", 7.5, 4)
sw, sh = getScreenResolution()
act = false
sync = false
pokaz = true
msg = "{c3b091}[{cc6666}Dream TP{c3b091}] {FFFFFF}"
local status = "dist (0 m)"


function teleportonfoot(arg)
	lockPlayerControl(false)

	if arg == "m" then
		blip, blipX, blipY, blipZ = getTargetBlipCoordinatesFixed()
	elseif arg == "c" then
		blip, blipX, blipY, blipZ = SearchMarker(getCharCoordinates(PLAYER_PED))
	else
		sampAddChatMessage(msg .. "incorrect parameter.", -1)

		return
	end

	lua_thread.create(function ()
		if act then
			sampAddChatMessage(msg .. "wait a while.", -1)

			return
		end

		if blip then
			charPosX, charPosY, charPosZ = getCharCoordinates(playerPed)

			sendOnfootSync(charPosX, charPosY, charPosZ, vehID)

			obsh = blipZ - charPosZ

			vect = getDistanceBetweenCoords3d(blipX, blipY, charPosZ, charPosX, charPosY, charPosZ)

			if getDistanceBetweenCoords3d(blipX, blipY, charPosZ, charPosX, charPosY, charPosZ) < 25 then
				return setCharCoordinates(playerPed, blipX, blipY, blipZ)
			end

			lockPlayerControl(true)

			sync = true
			act = true
			achkobus = blipY

			setCharCoordinates(playerPed, blipX, achkobus, blipZ)

			for achkobus = 0, math.floor(vect / 20), 1 do
				if getDistanceBetweenCoords3d(blipX, blipY, charPosZ, charPosX, charPosY, charPosZ) > 20 then
					wait(300)
					vectorX = blipX - charPosX
					vectorY = blipY - charPosY
					vectorZ = blipZ - charPosZ
					vect = vec(vectorX, vectorY, vectorZ)

					vect:normalize()

					charPosX = charPosX + vect.x * 20
					charPosY = charPosY + vect.y * 20
					charPosZ = charPosZ + vect.z * 20

					sendOnfootSyncing(charPosX, charPosY, charPosZ)

					status = "dist ("..getDistanceBetweenCoords3d(blipX, blipY, charPosZ, charPosX, charPosY, charPosZ).." m)"
				else
					wait(1200)

					sendOnfootSyncing(blipX, blipY, blipZ + 1)
					setCharCoordinates(playerPed, blipX, blipY, blipZ)
					sendSurf(charPosX + 20, charPosY + 20, charPosZ - 1, false)

					sync = true

					break
				end
			end

			sendOnfootSyncing(blipX, blipY, blipZ + 1)
			setCharCoordinates(playerPed, blipX, blipY, blipZ + 2)
			wait(200)
			lockPlayerControl(false)
			sampAddChatMessage(msg .. "success.", -1)

			sync = false

			wait(0)

			act = false
		end
	end)
end

function sendSurf(x, y, z, xuita)
	data = samp_create_sync_data("player")
	data.position = {
		x,
		y,
		z
	}

	if xuita then
		data.keysData = data.keysData + 1024
	end

	data.surfingOffsets = {
		0.41,
		0.41,
		0
	}
	data.surfingVehicleId = 2004

	data.moveSpeed = {
		0.41,
		0.41,
		0
	}

	data.send()
end

function sendOnfootSync(x, y, z, xuita)
	_, myId = sampGetPlayerIdByCharHandle(PLAYER_PED)
	data = allocateMemory(68)

	sampStorePlayerOnfootData(myId, data)
	setStructElement(data, 37, 1, 3, false)
	setStructFloatElement(data, 6, x, false)
	setStructFloatElement(data, 10, y, false)
	setStructFloatElement(data, 14, z, false)
	setStructElement(data, 62, 1, xuita, false)
	sampSendOnfootData(data)
	freeMemory(data)
end

function sampev.onSendPlayerSync(data)
	if sync then
		return false
	end
end

function sampev.onSetPlayerPos(data)
	if sync then
		return false
	end
end

function sampev.onSetPlayerPosFindZ(data)
	if sync then
		return false
	end
end

function sendOnfootSyncing(x, y, z)
	data = samp_create_sync_data("player")
	data.position = {
		x,
		y,
		z
	}
	data.moveSpeed = {
		0.51,
		0.51,
		0
	}

	data.send()
end

function getTargetBlipCoordinatesFixed()
    local bool, x, y, z = getTargetBlipCoordinates(); if not bool then return false end
    requestCollision(x, y); loadScene(x, y, z)
    local bool, x, y, z = getTargetBlipCoordinates()
    return bool, x, y, z
end

function SearchMarker(posX, posY, posZ)
    local ret_posX = 0.0
    local ret_posY = 0.0
    local ret_posZ = 0.0
    local isFind = false
    for id = 0, 31 do
        local MarkerStruct = 0
        MarkerStruct = 0xC7F168 + id * 56
        local MarkerPosX = representIntAsFloat(readMemory(MarkerStruct + 0, 4, false))
        local MarkerPosY = representIntAsFloat(readMemory(MarkerStruct + 4, 4, false))
        local MarkerPosZ = representIntAsFloat(readMemory(MarkerStruct + 8, 4, false))
        if MarkerPosX ~= 0.0 or MarkerPosY ~= 0.0 or MarkerPosZ ~= 0.0 then
            ret_posX = MarkerPosX
            ret_posY = MarkerPosY
            ret_posZ = MarkerPosZ
            isFind = true
        end
    end
    return isFind, ret_posX, ret_posY, ret_posZ
end

function samp_create_sync_data(sync_type, copy_from_player)
    local ffi = require 'ffi'
    local sampfuncs = require 'sampfuncs'
    -- from SAMP.Lua
    local raknet = require 'samp.raknet'
    --require 'samp.synchronization'

    copy_from_player = copy_from_player or true
    local sync_traits = {
        player = {'PlayerSyncData', raknet.PACKET.PLAYER_SYNC, sampStorePlayerOnfootData},
        vehicle = {'VehicleSyncData', raknet.PACKET.VEHICLE_SYNC, sampStorePlayerIncarData},
        passenger = {'PassengerSyncData', raknet.PACKET.PASSENGER_SYNC, sampStorePlayerPassengerData},
        aim = {'AimSyncData', raknet.PACKET.AIM_SYNC, sampStorePlayerAimData},
        trailer = {'TrailerSyncData', raknet.PACKET.TRAILER_SYNC, sampStorePlayerTrailerData},
        unoccupied = {'UnoccupiedSyncData', raknet.PACKET.UNOCCUPIED_SYNC, nil},
        bullet = {'BulletSyncData', raknet.PACKET.BULLET_SYNC, nil},
        spectator = {'SpectatorSyncData', raknet.PACKET.SPECTATOR_SYNC, nil}
    }
    local sync_info = sync_traits[sync_type]
    local data_type = 'struct ' .. sync_info[1]
    local data = ffi.new(data_type, {})
    local raw_data_ptr = tonumber(ffi.cast('uintptr_t', ffi.new(data_type .. '*', data)))
    -- copy player's sync data to the allocated memory
    if copy_from_player then
        local copy_func = sync_info[3]
        if copy_func then
            local _, player_id
            if copy_from_player == true then
                _, player_id = sampGetPlayerIdByCharHandle(PLAYER_PED)
            else
                player_id = tonumber(copy_from_player)
            end
            copy_func(player_id, raw_data_ptr)
        end
    end
    -- function to send packet
    local func_send = function()
        local bs = raknetNewBitStream()
        raknetBitStreamWriteInt8(bs, sync_info[2])
        raknetBitStreamWriteBuffer(bs, raw_data_ptr, ffi.sizeof(data))
        raknetSendBitStreamEx(bs, sampfuncs.HIGH_PRIORITY, sampfuncs.UNRELIABLE_SEQUENCED, 1)
        raknetDeleteBitStream(bs)
    end
    -- metatable to access sync data and 'send' function
    local mt = {
        __index = function(t, index)
            return data[index]
        end,
        __newindex = function(t, index, value)
            data[index] = value
        end
    }
    return setmetatable({send = func_send}, mt)
end

function teleport(arg)
	if isCharInAnyCar(PLAYER_PED) then
		teleportcar(arg)
	else
		teleportonfoot(arg)
	end
end

function main()
	while not isSampAvailable() do
		wait(0)
	end

	renderbar()

	sampRegisterChatCommand("dtp", teleport)
	wait(-1)
end

function givecar()
	for i = 0, 2000 do
		_, car = sampGetCarHandleBySampVehicleId(i)

		if _ then
			xh, yh, zh = getCharCoordinates(playerPed)
			xh1, yh1, zh1 = getCarCoordinates(car)
			vod = getDriverOfCar(car)
			dds = getDistanceBetweenCoords3d(xh, yh, zh, xh1, yh1, zh1)

			return i
		end
	end

	return "nope"
end

function renderbar()
	lua_thread.create(function()
		while true do
			wait(0)
			if pokaz then
				if act then
					renderFontDrawText(font, "Status: {ffff99}teleporting "..status, 10, sw / 3, 4294967295.0, 4294967295.0)
				else
					-- renderFontDrawText(font, "Status: {ffff99}Available", 10, sw / 3, 4294967295.0, 4294967295.0)
				end
			end
		end
	end)
end




function teleportcar(arg)
	lua_thread.create(function ()
		if arg == "m" then
			blip, bx, by, bz = getTargetBlipCoordinatesFixed()
		elseif arg == "c" then
			blip, bx, by, bz = SearchMarker(getCharCoordinates(PLAYER_PED))
		end

		if (arg == "m" or arg == "c") and isCharInAnyCar(PLAYER_PED) then
			if blip then
				x, y, z = getCharCoordinates(playerPed)
				local dist = getDistanceBetweenCoords3d(bx, by, bz, x, y, z)
				obsh = bz - z

				if dist < 30 then
					return setCharCoordinates(playerPed, bx, by, bz)
				end

				syncc = true
				tppp = true
				act = true
				kol = math.floor(dist / 30)
				abobus = by

				setCharCoordinates(playerPed, bx, abobus, bz)

				for abobus = 0, kol do
					dis = getDistanceBetweenCoords3d(bx, by, bz, x, y, z)

					if dis > 30 then
						wait(400)
						vectorX = bx - x
						vectorY = by - y
						vectorZ = bz - z
						vect = vec(vectorX, vectorY, vectorZ)

						vect:normalize()

						x = x + vect.x * 30
						y = y + vect.y * 30
						z = z + vect.z * 30

						sendVehicleSyncing(x, y, z)
						status = "dist ("..dis.." m)"
					else
						sync = true

						sendVehicleSyncing(bx, by, bz)
						sendVehicleSyncing(bx + 30, by + 30, bz + 2)
						wait(666)
						sampAddChatMessage(msg .. "success.", -1)
						sendVehicleSyncing(bx + 1, by + 1, bz + 2)
						sampForceVehicleSync()
						sendVehicleSyncing(bx, by, bz + 2)

						act = false
						tppp = false
						sync = false

						break
					end
				end
			end
		else
			sampAddChatMessage(msg .. "incorrect parameter.", -1)
		end
	end)
end

function sendVehicleSyncing(x, y, z)
	data = samp_create_sync_data("vehicle")
	data.position = {
		x,
		y,
		z
	}
	data.moveSpeed = {
		0.3,
		0.3,
		0.3
	}

	data.send()
end

function sampev.onSetVehiclePosition(data)
	if syncc then
		return false
	end
end

function sampev.onSetVehiclePos(data)
	if syncc then
		return false
	end
end 

Lua online compiler

Write, Run & Share Lua code online using OneCompiler's Lua online compiler for free. It's one of the robust, feature-rich online compilers for Lua language, running the latest Lua version 5.4. Getting started with the OneCompiler's Lua editor is easy and fast. The editor shows sample boilerplate code when you choose language as Lua and start coding.

Taking inputs (stdin)

OneCompiler's Lua online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Lua program which takes name as input and prints hello message with your name.

name = io.read("*a")
print ("Hello ", name)

About Lua

Lua is a light weight embeddable scripting language which is built on top of C. It is used in almost all kind of applications like games, web applications, mobile applications, image processing etc. It's a very powerful, fast, easy to learn, open-source scripting language.

Syntax help

Variables

  • By default all the variables declared are global variables
  • If the variables are explicitly mentioned as local then they are local variables.
  • Lua is a dynamically typed language and hence only the values will have types not the variables.

Examples

-- global variables
a = 10

-- local variables

local x = 30
Value TypeDescription
numberRepresents numbers
stringRepresents text
nilDifferentiates values whether it has data or not
booleanValue can be either true or false
functionRepresents a sub-routine
userdataRepresents arbitary C data
threadRepresents independent threads of execution.
tableCan hold any value except nil

Loops

1. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition)
do
--code
end

2. Repeat-Until:

Repeat-Until is also used to iterate a set of statements based on a condition. It is very similar to Do-While, it is mostly used when you need to execute the statements atleast once.

repeat
   --code
until( condition )

3. For:

For loop is used to iterate a set of statements based on a condition.

for init,max/min value, increment
do
   --code
end

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increase re-usuability and modularity.

optional_function_scope function function_name( argument1, argument2, argument3........, argumentn)
--code
return params with comma seperated
end