Jump to content

How do you sync ped


Recommended Posts

Could you possibly show me an example/point me on the right direction on how to sync this?

  
addEventHandler("onClientRender", root, 
function() 
  
    local botid 
  
    for _, bot in ipairs(getElementsByType("ped")) do 
     
        botid = getElementData(bot, "bot.id") 
     
        if(botid ~= false) then 
         
            local px, py, pz = getElementPosition(localPlayer) 
            local x, y, z = getElementPosition(bot) 
            local rot = findRotation(x, y, px, py) 
            local dist = getDistanceBetweenPoints3D(x, y, z, px, py, pz) 
            local mode = getElementData(bot, "bot.mode") 
            local running = getPedControlState(bot, "forwards") 
            local sprinting = getPedControlState(bot, "sprint") 
            local jumping = getPedControlState(bot, "jump") 
            local health = getElementHealth(bot) 
            local clearpath = isLineOfSightClear(px, py, pz, x, y, z, true, true, false, true, false, true, false) 
             
             
            if((clearpath == false) and (jumping == false) and ((getTickCount() - lastjumptime) < (500/getGameSpeed()))) then 
             
                outputChatBox("Jump!") 
                setPedAnimation(bot) 
                setPedControlState(bot, "jump", true) 
             
            end 
             
            if((getTickCount() - lastjumptime) > (500/getGameSpeed())) then 
  
                setPedControlState(bot, "jump", false) 
                lastjumptime = getTickCount() 
  
            end 
             
            if(health < 1) then setTimer(destroyElement, 2000, 1, bot) end 
             
            if(mode == "follow") then 
                 
                setPedRotation(bot, rot) 
                setPedLookAt(bot, px, py, pz) 
                 
                -- Sprinting = Running and Sprinting 
                -- Running = Running 
                 
                if(dist < 1) then -- If they are too close. 
                 
                    if((running) or (running and sprinting)) then -- If they are either running or sprinting. 
                        --Stop them. 
                        outputChatBox("Should stop") 
                        setPedControlState(bot, "sprint", false)  
                        setPedControlState(bot, "forwards", false) 
                     
                    end 
                     
                    setPedControlState(bot, "fire", true) 
                     
                 
                elseif(dist > 15.0) then -- If they are too far. 
                     
                    if(sprinting == false) then -- If they are not sprinting. 
                        --Sprint! 
                        outputChatBox("Should sprint.") 
                        setPedControlState(bot, "sprint", true) 
                        setPedControlState(bot, "forwards", true) 
                     
                    end 
                     
                    setPedControlState(bot, "fire", false) 
                                     
                else -- If they are not too close or too far. 
                 
                    if((running and sprinting) or (running == false)) then -- If they are sprinting or idle. 
                        --Run! 
                        outputChatBox("Should run.") 
                        setPedControlState(bot, "sprint", false)  
                        setPedControlState(bot, "forwards", true) 
                         
                    end 
                     
                    setPedControlState(bot, "fire", false) 
                 
                end 
                 
            end 
             
        end 
     
    end 
  
end) 
  

Link to comment

Alright managed to sync the ped but I still have a problem. The bot shoots in an awkward angle and I believe it's happening because of the animation or something. The rotation, the bot is facing is alright but the bot doesn't shoot at the player's body location.

Server:

  
local id = 0 
  
addCommandHandler("createbot",  
function(player, command, skin) 
  
    skin = skin or 28 
    local x, y, z = getElementPosition(player) 
    id = id + 1 
    local bot = createPed(28, x, y, z) 
    setElementData(bot, "bot.id", id) 
    setElementData(bot, "bot.mode", "none") 
    setElementData(bot, "bot.action", "none") 
    setElementData(bot, "bot.animation", "none") 
    setElementData(bot, "bot.clear", false) 
    setElementData(bot, "bot.target", player) 
    outputChatBox("Bot Created: " .. tostring(id), player) 
     
end) 
  
addCommandHandler("givewep", 
function(player, command, botid, weapon) 
  
    botid = botid or 1 
    weapon = weapon or "Knife" 
    local bot = botGet(botid) or outputChatBox("Invalid Bot.", player) 
    local weaponid = getWeaponIDFromName(weapon) or outputChatBox("Invalid Weapon.", player) 
  
    if(bot and weaponid) then 
     
        giveWeapon(bot, weaponid, 99999, true) 
        outputChatBox("Bot " .. tostring(botid) .. " given weapon " .. weapon, player) 
         
    end 
  
end) 
  
addCommandHandler("setfollow",  
function(player, command, botid) 
  
    botid = botid or 1 
    local bot = botGet(botid) or outputChatBox("Invalid Bot.", player) 
    local x, y, z = getElementPosition(player) 
    local rot = getPedRotation(player) 
     
    if(bot) then 
     
        setPedAnimation(bot) 
        setElementData(bot, "bot.mode", "follow") 
        outputChatBox("Bot " .. tostring(botid) .. " set to follow you", player) 
     
    end 
     
end) 
  
  
addEventHandler("onResourceStart", resourceRoot, 
function() 
  
    setTimer( 
        function()  
         
            local botid 
            local px, py, pz 
            local x, y, z 
            local rot 
            local dist 
            local mode 
            local action 
            local animation 
            local clear 
            local target 
            local health 
  
            for _, bot in ipairs(getElementsByType("ped")) do 
             
                botid = getElementData(bot, "bot.id") 
             
                if(botid ~= false) then 
                 
                    mode = getElementData(bot, "bot.mode") 
                    action = getElementData(bot, "bot.action") 
                    animation = getElementData(bot, "bot.animation") 
                    clear = getElementData(bot, "bot.clear") 
                    target = getElementData(bot, "bot.target") 
                    px, py, pz = getElementPosition(target) 
                    x, y, z = getElementPosition(bot) 
                    rot = findRotation(x, y, px, py) 
                    dist = getDistanceBetweenPoints3D(x, y, z, px, py, pz) 
                    health = getElementHealth(bot) 
                     
                    setPedRotation(bot, rot) 
                    --setPedLookAt(bot, x, y, z) 
                     
                    if((action == "none") and (animation ~= "none")) then 
                     
                        outputChatBox("Playing no animation", target) 
                        setElementData(bot, "bot.animation", "none") 
                        setPedAnimation(bot) 
                     
                    elseif(action == "run") then 
                     
                        outputChatBox("Playing running animation", target) 
                        setElementData(bot, "bot.animation", "run") 
                        setPedAnimation(bot, "ped", "run_civi") 
                         
                    elseif(action == "sprint") then 
                     
                        outputChatBox("Playing sprinting animation", target) 
                        setElementData(bot, "bot.animation", "sprint") 
                        setPedAnimation(bot, "ped",  "sprint_civi") 
                         
                    end 
                     
                    if(mode == "follow") then 
                     
                        if(dist > 5 and dist < 30) then -- Running Range 
                         
                            outputChatBox("Should Run", target) 
                            if(clear) then -- Path Clear 
                             
                                outputChatBox("Running!", target) 
                                setElementData(bot, "bot.action", "run") 
                             
                            end 
                         
                        elseif(dist > 30) then  
                         
                            outputChatBox("Should Sprint", target) 
                            if(clear) then 
                             
                                outputChatBox("Sprinting!", target) 
                                setElementData(bot, "bot.action", "sprint") 
                             
                            end 
                             
                        else  
                         
                            outputChatBox("Should shoot", target) 
                            setElementData(bot, "bot.action", "none") 
                             
                            if(getPedWeapon(bot) > 0) then 
                             
                                triggerClientEvent("cFire", bot) 
                             
                            end 
                         
                        end 
                     
                    end 
                     
                end 
             
            end 
  
         
        end 
    , 50, 0) 
     
end) 
  
  
function botGet(botid) 
  
    for _, bot in ipairs(getElementsByType("ped")) do if(getElementData(bot, "bot.id") == tonumber(botid)) then return bot end end 
     
    return nil 
  
end 
function findRotation(x1, y1, x2, y2) 
  
  local t = -math.deg(math.atan2(x2-x1,y2-y1)) 
  if t < 0 then t = t + 360 end; 
  return t; 
  
end 
  

Client:

  
--[[addEventHandler("onClientPedDamage", root, 
function(attacker, weapon, bodypart) 
  
    if(getElementData(attacker, "bot.id") ~= false) then 
     
        if(weapon == getWeaponIDFromName("Knife")) then 
         
            if(bodypart == 9) then setPedHeadless(source, true) 
            else setElementHealth(source, getElementHealth(source) - 40)  
            end 
         
        end 
     
    else 
     
        if(bodypart == 9) then setPedHeadless(source, true) setElementHealth(source, 0) end 
     
    end 
         
    cancelEvent() 
         
end)--]] 
  
addEventHandler("onClientRender", root, 
function() 
  
    local botid 
    local px, py, pz 
    local x, y, z 
  
    for _, bot in ipairs(getElementsByType("ped")) do 
     
        botid = getElementData(bot, "bot.id") 
        px, py, pz = getElementPosition(localPlayer) 
        x, y, z = getElementPosition(bot) 
     
        if(botid ~= false) then 
         
            local clearpath = isLineOfSightClear(px, py, pz, x, y, z, true, true, false, true, false, true, false) 
             
            if(clearpath == false) then setElementData(bot, "bot.clear", false)  
            else setElementData(bot, "bot.clear", true) end 
         
        end  
         
    end 
end) 
  
addEvent("cFire", true) 
addEventHandler("cFire", root,  
function() 
  
    setPedControlState(source, "fire", true) 
    setTimer(setPedControlState, 800, 1, source, "fire", false) 
  
end) 
  

Link to comment
  • Moderators

Elementdata you only use on server side will lagg you bandwidth. It will give you more code+time, but it will reduce lots of ping lagg in the future. Since you use bots, it will require double+ of players their internet speed. Add to much Element data and you have teleporting peds.

Use instead Tables:

local botsTable[bot] = {} 
local botsTable[bot]["bot.animation"] = true -- or anim etc. 
  
if botsTable[bot]["bot.animation"]  then 
local anim = botsTable[bot]["bot.animation"]   
end 

 

-- you can also put it in "array"(mta does have a kind of array, but it also not array) when you need it. 
  
table.insert (botsTable,bot)  
--or  
local i = 1 
botsTable[i]=bot -- i = index 1,2,3,4,5  
--[[NOTE:]] botsTable[#botsTable+1] = bot  --[[]] table.insert(botsTable,bot) 
  
botsTable[i]["bot.animation"]= "lol" 
  
for i, bot in ipairs(botsTable) do -- botsTable[i] = bot 
if isElement(bot) and thisBot == bot then 
 outputChatBox(botsTable[i]["bot.animation"]) 
botsTable[i]["bot.animation"]= "new lol" 
  
end 
end 
  
  
  
-- clear elements when they got destroyed, don't loop while to much. While can crash your server when your code might be incorrect. 
for i, bot in ipairs(botsTable) do  
while botsTable[i] and not isElement(bot) do 
table.remove (botsTable, i) 
end 
end 

 

Link to comment
  • Moderators

Yes that is the best way.

- The information that have been moved from client to server is 5 kb (dunno, but lets make this sample big)

- 25 players are in the server.

Elementdata:

elementdata * playersInServer = bandwidth of all players. 25 * 5kb = 125kb! Server data travel. ( 24 players client download)

Trigger:

tabledata * 1 = bandwidth of one player. 5 * 1 = 5 kb Server data travel. (no client download)

But nobody is ever listening... -_-"

BTW(client):

https://wiki.multitheftauto.com/wiki/SetPedAimTarget

-- get the ped rotation and send this to the server, not sure if the syncs are 100%.

even so you can calculate the syncs by the client, move the index number through the server. That would be the smallest way.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...