Помощь по Lua

mitaminka5

Новичок
Автор темы
6
0
Это бот для машиниста радмир, проблема в том что, при приблежении к станции он не плавно тормозит, а моментально остановляеться, не подскажите как это пофиксить, т.к я в этом не вообще не шарю
Lua:
local ev = require('lib.samp.events')

local state = false
local canMove = false
local dissync = false

local speed = {
    min = 0,
    max = 0
}

local checkpoint = {
    position = {
        x = 0,
        y = 0,
        z = 0
    },
    isFinish = false
}

local key = 0

function onReceivePacket(id, bs)
    if id == 215 then
        raknetBitStreamIgnoreBits(bs, 8)
        if raknetBitStreamReadInt16(bs) == 2 then
            local a = raknetBitStreamReadInt32(bs)
            local e = {}
            for i = 1, raknetBitStreamReadInt8(bs) do
                local l = raknetBitStreamReadInt32(bs)
                table.insert(e, raknetBitStreamReadString(bs, l))
            end
            if table.getn(e) > 0 then
                local text = e[1]
                local minSpeed, maxSpeed = text:match('interface%(\'Machinist\'%)%.setSpeed%(\'%["(%d+)%-(%d+)", %d+%]\'%)')
                if minSpeed ~= nil and maxSpeed ~= nil then
                    speed.min = tonumber(minSpeed)
                    speed.max = tonumber(maxSpeed)
                end
            end
        end
    end
end

function main()
    repeat wait(0) until isSampAvailable()
    sampRegisterChatCommand('train', function()
        state = not state
        sampAddChatMessage(state and 'on' or 'off', -1)
    end)
    sampRegisterChatCommand('slp', function()
        local x, y, z = getCharCoordinates(PLAYER_PED)
        setCharCoordinates(PLAYER_PED,x,y,z+1.5)
    end)
    while true do
        wait(0)
        if state and isCharInAnyTrain(PLAYER_PED) then
            local curSpeed = getCarSpeedKmh(getCarCharIsUsing(PLAYER_PED))
            local x, y, z = getCharCoordinates(PLAYER_PED)
            local moveDirection = getCarSpeed(getCarCharIsUsing(PLAYER_PED)) > 0 and 1 or (getCarSpeed(getCarCharIsUsing(PLAYER_PED)) < 0 and -1 or 0)
            if canMove then
                if checkpoint.isFinish then
                    local dist = getDistanceBetweenCoords3d(x, y, z, checkpoint.position.x, checkpoint.position.y, checkpoint.position.z)
                    if dist <= 3 then
                        setTrainSpeed(getCarCharIsUsing(PLAYER_PED), math.max(0, getCarSpeed(getCarCharIsUsing(PLAYER_PED)) / 1.1 - 1))
                    else
                        setGameKeyState(16, 255 * (curSpeed > (speed.min+speed.max)/2 and -1 or 1))
                    end
                else
                    setGameKeyState(16, 255 * (curSpeed > (speed.max+speed.min)/2 and -1 or 1))
                end
            end
            key = moveDirection == 1 and 8 or (moveDirection == -1 and 32 or 0)
        end
    end
end

function coordPos(x, y, z)
    lua_thread.create(function()
        local mx, my, mz = getCharCoordinates(PLAYER_PED)
        dissync = true
        local k = 0
        while getDistanceBetweenCoords3d(x, y, z, mx, my, mz) > 1 and isCharOnFoot(PLAYER_PED) do
            if mx > x then mx = mx - 1 else mx = mx + 1 end
            if my > y then my = my - 1 else my = my + 1 end
            if mz > z then mz = mz - 1 else mz = mz + 1 end
            sendPacket(mx, my, mz)
            k = k + 1
            if k > 3 then
                k = 0
            end
            local s = 'teleporting by AkeGGa'
            for i = 1, k do
                s = s .. '.'
            end
            printStringNow(s, 110)
            wait(100)
        end
        setCharCoordinates(PLAYER_PED, x, y, z)
        dissync = false
    end)
end

function sendPacket(x, y, z)
    local bs = raknetNewBitStream()
    raknetBitStreamWriteInt8(bs, 207)
    raknetBitStreamWriteInt16(bs, 0)
    raknetBitStreamWriteInt16(bs, 0)
    raknetBitStreamWriteInt16(bs, 0)
    raknetBitStreamWriteFloat(bs, x)
    raknetBitStreamWriteFloat(bs, y)
    raknetBitStreamWriteFloat(bs, z)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteInt8(bs, getCharHealth(PLAYER_PED))
    raknetBitStreamWriteInt8(bs, getCharArmour(PLAYER_PED))
    raknetBitStreamWriteInt8(bs, getCurrentCharWeapon(PLAYER_PED))
    raknetBitStreamWriteInt8(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteFloat(bs, 0)
    raknetBitStreamWriteInt16(bs, 0)
    raknetBitStreamWriteInt16(bs, 0)
    raknetBitStreamWriteInt16(bs, 0)
    raknetSendBitStreamEx(bs, 2, 8, 0)
    raknetDeleteBitStream(bs)
end

function getCarSpeedKmh( vehicleTarget )
    if not vehicleTarget or type( vehicleTarget ) ~= 'number' then return false end
    if not doesVehicleExist( vehicleTarget ) then return false end
    local x, y, z = getCarSpeedVector( vehicleTarget )
    if not x or not y or not z then x, y, z = 0, 0, 0 end
    local kmh = math.floor( (math.sqrt( (x*x) + (y*y) + (z*z) ) * 180) / 100 ) -- KM/H
    local mph = math.floor( (math.sqrt( (x*x) + (y*y) + (z*z) ) * 180) / 1.609344 / 100 ) -- MPH
    return kmh * 2
end

function ev.onSetRaceCheckpoint(type, pos, nextPos, size)
    canMove = true
    checkpoint.position = pos
    checkpoint.isFinish = type == 1
end

function ev.onSetCheckpoint(pos, radius)
    if state and isCharOnFoot(PLAYER_PED) then
        coordPos(pos.x, pos.y, pos.z)
    end
end

function ev.onDisableRaceCheckpoint()
    canMove = false
end

function ev.onSendVehicleSync(data)
    -- 8 - вперед 32 - назад
    data.keysData = key
end

function ev.onSendPlayerSync(data)
    if dissync then return false end
end

function Sync(data)
    -- 8 - вперед 32 - назад
    data.keysData = key
end

function ev.onSendPlayerSync(data)
    if dissync then return false end
end
 

Вложения

  • train.lua
    5.6 KB · Просмотры: 8