Unsolved Ai traffic personality

Discussion in 'Mod Support' started by SupraTech, Apr 27, 2025 at 7:03 PM.

  1. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    Hi there. I was trying to make a mod for the ai that adds personalities for the ai drivers

    Normal: Drives normally, obeying the speed limit and with experience. Rarely causes accidents.

    Elderly/Beginner: Drives very slowly and has difficulty parking and maneuvering. Often causes minor accidents, such as fender benders, due to inexperience.

    Karen: Drives slightly over the speed limit, tends to get very angry, cuts off other drivers, and drives recklessly. Often causes moderate accidents and always flees if it is their fault.

    Aggressive 1: Always exceeds the speed limit and drives recklessly. They are inexperienced and can cause serious accidents. They also often weave through traffic and avoid traffic jams by driving on the wrong side of the road.

    Aggressive 2: Always exceeds the speed limit and drives recklessly. They are very experienced and don't cause accidents, but they do cause a few scares. They also tend to weave through traffic and avoid traffic jams by driving on the wrong side of the road.

    The thing is that I need help with the code. Ik this may seem stupid if you are an experienced modder, but I'm not the most skilled one... This is the code I wrote [KEEP IN MIND THAT STRUCTURE VARIATES FROM NOTEPAD++ TO HERE]

    -- Define AI personalities
    local personalities = {
    "aggressive",
    "defensive",
    "normal"
    }

    -- Function to initialize AI personalities for vehicles
    local function onAIInit(vehicle)
    -- Add AI personality based on a random choice
    local personality = personalities[math.random(#personalities)]
    vehicle.AI_Personality = personality
    print("AI Personality assigned: " .. personality)

    -- Set a base speed and behavior
    vehicle:setThrottle(1) -- Full speed for demonstration

    -- Start behavioral updates
    checkAIBehaviors(vehicle)
    end

    -- Function to check AI behaviors based on the personality
    local function checkAIBehaviors(vehicle)
    local personality = vehicle.AI_Personality

    if personality == "aggressive" then
    aggressiveBehavior(vehicle)
    elseif personality == "defensive" then
    defensiveBehavior(vehicle)
    else
    normalBehavior(vehicle)
    end
    end

    function aggressiveBehavior(vehicle)
    -- Simple aggressive behavior, maintaining high speed,
    -- and attempting to overtake other vehicles
    vehicle:setThrottle(1) -- Full throttle

    -- Logic to find nearby vehicles
    local nearbyVehicles = findNearbyVehicles(vehicle)
    for _, otherVehicle in ipairs(nearbyVehicles) do
    if otherVehicle ~= vehicle then
    -- Logic to cut off other vehicles
    if vehicle:getSpeed() > otherVehicle:getSpeed() then
    vehicle:setSteering(-1) -- Steer left, example
    end
    end
    end
    end

    function defensiveBehavior(vehicle)
    -- Defensive behavior, maintaining safe speed and distance
    vehicle:setThrottle(0.5) -- Half throttle

    -- Logic to slow down when close to other vehicles
    local nearbyVehicles = findNearbyVehicles(vehicle)
    for _, otherVehicle in ipairs(nearbyVehicles) do
    if otherVehicle ~= vehicle then
    if vehicle:getDistance(otherVehicle) < 20 then -- 20 meters as an example
    vehicle:setThrottle(0.2) -- Slow down
    end
    end
    end
    end

    function normalBehavior(vehicle)
    -- Normal behavior, maintaining a balanced speed
    vehicle:setThrottle(0.7) -- Maintain a decent speed
    end

    function findNearbyVehicles(vehicle)
    local nearbyVehicles = {}
    local position = vehicle:getPosition()

    -- Check a certain radius (e.g., 100 meters) for other vehicles
    local radius = 100
    for _, otherVehicle in pairs(be:getAllVehicles()) do
    if otherVehicle ~= vehicle then
    local otherPos = otherVehicle:getPosition()
    local distance = (position - otherPos):length()

    if distance <= radius then
    table.insert(nearbyVehicles, otherVehicle)
    end
    end
    end
    return nearbyVehicles
    end

    -- Hook into the AI initialization event
    addEventHandler('AI.onInit', function(vehicle)
    onAIInit(vehicle)
    end)
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Ik know this may seem STUPID but I need help pls.
     
    #1 SupraTech, Apr 27, 2025 at 7:03 PM
    Last edited: Apr 27, 2025 at 7:25 PM
  2. neymar_19932

    neymar_19932
    Expand Collapse

    Joined:
    Apr 17, 2025
    Messages:
    91
  3. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    hi. Do you know how to do this??
     
  4. neymar_19932

    neymar_19932
    Expand Collapse

    Joined:
    Apr 17, 2025
    Messages:
    91
    no
     
  5. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    ok
    --- Post updated ---
    [KEEP IN MIND THAT STRUCTURE OF THE CODE VARIATES FROM NOTEPAD++ TO HERE]
     
  6. jamesthecarguy

    jamesthecarguy
    Expand Collapse

    Joined:
    Sep 27, 2024
    Messages:
    186
  7. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    yes, but unfunctional
     
  8. jamesthecarguy

    jamesthecarguy
    Expand Collapse

    Joined:
    Sep 27, 2024
    Messages:
    186
    i have some things like:

    Street Racer - Always has some sort of sporty car and tries to race with you, gets mad if he loses.
    Reckless - A driver who runs red lights and doesn't use turn signals, also goes a little bit off the speed limit.
    Careless - A driver who does noy have any care in the world, speeds all the time.
    Road Rager - Gets mad at speeders and cuts them off to try and "teach the them a lesson".
    --- Post updated ---
    Gets mad if you bump them.
     
    • Like Like x 1
  9. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    Thats basically Karen and aggressive 1 and 2
     
  10. SynthethicMorgan?

    SynthethicMorgan?
    Expand Collapse

    Joined:
    Feb 28, 2024
    Messages:
    336
    i hope that this is going to be a mod
     
  11. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    I'm planning on it
     
  12. jamesthecarguy

    jamesthecarguy
    Expand Collapse

    Joined:
    Sep 27, 2024
    Messages:
    186
    one new one that i came up with

    Civilian Police - Acts like BeamNG Police.
    Drunk - Swerves across the road and runs red lights most of the time.
    New - A little bit off the speed limit and sometimes doesn't use turn signals sometimes.
     
  13. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    Great ty!
     
  14. elimiel2

    elimiel2
    Expand Collapse

    Joined:
    Feb 12, 2025
    Messages:
    636
    1. In training: often stops randomly
    2. self drive, drives perfectly, sometimes stops
     
  15. SupraTech

    SupraTech
    Expand Collapse

    Joined:
    Jan 6, 2025
    Messages:
    197
    Ok! I'll add them. But I think that's a bit too much for first release. I'll put the in next update maybe
    --- Post updated ---
    But the thing is, I can't get the script to work... I can't figure it out...
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice