Ближайшие id в стриме

vvvlvs

Новичок
Автор темы
20
0
Версия MoonLoader
.026-beta
Помогите реализовать, нужно вывести ближайшие 3 id, Либо же альтернативой может как - то сделать выборку те кто со мной находятся в автомобиле, и не важно за рулём я или нет. Короче говоря, это для докладов в /r


Lua:
require "lib.moonloader"
function main()
  if not isSampfuncsLoaded() or not isSampLoaded() then return end
  while not isSampAvailable() do wait(100) end
  while true do
    wait(0)
    function getClosestPlayerId()
      local minDist = 999
      local closestId = -1
      local x, y, z = getCharCoordinates(PLAYER_PED)
      for i = 0, 999 do
          local streamed, pedID = sampGetCharHandleBySampPlayerId(i)
          if streamed then
              local xi, yi, zi = getCharCoordinates(pedID)
              local dist = math.sqrt( (xi - x) ^ 2 + (yi - y) ^ 2 + (zi - z) ^ 2 )
              if dist < minDist then
                  minDist = dist
                  closestId = i
              end
          end
      end
      return closestId
    end
  end
end


Это для одного ближайшего, а надо 4
 
Последнее редактирование модератором:

Cosmo

Известный
Друг
646
2,606
bool result, table table = getClosestPlayers(int count):
function getClosestPlayers(count)
    local arr = {}
    local myX, myY, myZ = getCharCoordinates(playerPed)
    for _, char in pairs(getAllChars()) do
        local plX, plY, plZ = getCharCoordinates(char)
        local dist = math.floor(getDistanceBetweenCoords3d(myX, myY, myZ, plX, plY, plZ))
        local result, playerId = sampGetPlayerIdByCharHandle(char)
        local selfId = select(2, sampGetPlayerIdByCharHandle(playerPed))
        if result and playerId ~= selfId then table.insert(arr, {dist, playerId}) end   
    end
    if #arr > 0 then
        local count = count and tonumber(count) or 1
        table.sort(arr, function(a, b) return (a[1] < b[1]) end)
        while #arr > tonumber(count) do
            table.remove(arr, #arr)
        end
        return true, arr
    end
    return false
end

Пример использования:
sampRegisterChatCommand('closest', function(...)
    local result, tpl = getClosestPlayers(...)
    if result then
        sampAddChatMessage('Ближайшие '.. #tpl ..' игроков к вам: ', -1)
        for k, v in pairs(tpl) do
            sampAddChatMessage(sampGetPlayerNickname(v[2])..' - Dist: '..v[1], -1)
        end
    else
        sampAddChatMessage('Игроков рядом нет', -1)   
    end
end)