Guest User

dumb_fighting_bots.lua

a guest
May 3rd, 2015
335
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Rudimentary Bot combat script by BFG9000
  2. --This is crap don't use this in a serious context pls
  3. --Sorting function taken from Sanic NPC by Xyxen
  4. if SERVER then
  5.  
  6. local function IsValidBotTarget(ent)
  7.     if (!ent:IsValid()) then return false end
  8.  
  9.     return ((ent:IsPlayer() and ent:Alive()) or
  10.             (ent:IsNPC() and ent:Health() > 0))
  11.  
  12. end
  13.  
  14. local function GetNearestBotTargetFromTable(table, bot)
  15.     local maxAcquireDist = 10000
  16.     local maxAcquireDistSqr = math.pow(maxAcquireDist, 2)
  17.     //local acquirableEntities = ents.FindInSphere(self:GetPos(), maxAcquireDist)
  18.     local target = nil
  19.  
  20.     for _, ent in pairs(table) do
  21.         if (not IsValidBotTarget(ent)) then continue end
  22.  
  23.         local distSqr = ent:GetPos():DistToSqr(bot:GetPos())
  24.         if distSqr < maxAcquireDistSqr then
  25.             target = ent
  26.             maxAcquireDistSqr = distSqr
  27.         end
  28.     end
  29.  
  30.     return target
  31.  
  32. end
  33.  
  34. local function BotSetTarget(bot)
  35.     bot.PotentialTargets = ents.FindInSphere(bot:GetPos(), 9001)
  36.     //for k,v in pairs(bot.PotentialTargets) do if not v:IsPlayer() then table.remove(bot.PotentialTargets, k) end end
  37.     bot.ViableTargets = {}
  38.         for k,v in pairs(bot.PotentialTargets) do if IsValidBotTarget(v) then if bot:Visible(v) then table.ForceInsert(bot.ViableTargets, v) end end end
  39.     table.RemoveByValue(bot.ViableTargets, bot)
  40.            
  41.  
  42.     bot.CurrentTarget = GetNearestBotTargetFromTable(bot.ViableTargets, bot)
  43.     //bot.CurrentTarget = bot.ViableTargets[1]
  44.     if IsValid(bot.CurrentTarget) then
  45.         if bot.CurrentTarget:IsPlayer() then
  46.             if !bot.CurrentTarget:Alive() then
  47.                 bot.CurrentTarget = table.Random(bot.ViableTargets)
  48.             end
  49.         elseif bot.CurrentTarget:IsNPC() then
  50.             if not (bot.CurrentTarget:Health() < 1) then
  51.                 bot.CurrentTarget = table.Random(bot.ViableTargets)
  52.             end
  53.         end
  54.     end
  55.     //print(bot.CurrentTarget)
  56. end
  57.  
  58. local NextTarget = CurTime()
  59. local TargetUpdateRate = 4
  60. local function BotTargetThink()
  61.     if NextTarget <= CurTime() then
  62.     NextTarget = CurTime() + TargetUpdateRate
  63.         for id, bot in pairs(player.GetBots()) do
  64.             BotSetTarget(bot)
  65.         end
  66.  
  67.     //NextTarget = CurTime() + TargetUpdateRate
  68.     end
  69. end
  70.  
  71. hook.Add("Think", "BFG Bot Targeting", BotTargetThink)
  72.  
  73. local NextThink = CurTime()
  74. local ThinkRate = .1
  75. local function BotThink()
  76.     //if NextThink <= CurTime() then
  77.     NextThink = CurTime() + ThinkRate
  78.         for id, bot in pairs(player.GetBots()) do
  79.             if IsValid(bot.CurrentTarget) and (bot.CurrentTarget != self and bot.CurrentTarget != nil) then
  80.                 bot.TargetAimDir = ((bot.CurrentTarget:GetPos() + Vector(0,0,35)) - (bot:GetPos() + Vector(0,0,35))):Angle() - bot:GetPunchAngle()
  81.                 //bot:SetEyeAngles( bot.TargetAimDir ) --aimbot
  82.                 bot:SetEyeAngles( BFGApproach3DAngles( bot:EyeAngles(), bot.TargetAimDir, (bot.TargetAimDir.yaw - bot:EyeAngles().yaw)/6.5))
  83.  
  84.                 //print(bot.TargetAimDir)
  85.             else
  86.                 //bot:SetEyeAngles( Angle( 30,30,30) )
  87.                
  88.             end
  89.         end
  90.  
  91.     //end
  92.    
  93. end
  94. hook.Add("Think", "BFG Bot Thinking", BotThink)
  95.  
  96.  
  97. local function UpdateBotTarget(victim, inflictor, attacker)
  98.     if attacker:IsNPC() or not (attacker:IsPlayer()) then return end
  99.    
  100.     if attacker:IsBot() then
  101.     //print(attacker:Nick() .. ": Tango Down!")
  102.         if not IsValid(attacker.CurrentTarget) then return end
  103.         if not attacker.CurrentTarget == victim then return end
  104.         BotSetTarget(attacker)
  105.         //print("New target acquired")
  106.     end
  107. end
  108. hook.Add("PlayerDeath", "BFG_UpdateBotTargets", UpdateBotTarget)
  109.  
  110.  
  111. end --IF SERVER THEN
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. function BFGApproach3DAngles( CurrentAng, TargetAng, Rate )
  121.     local pitch = math.ApproachAngle( CurrentAng.pitch, TargetAng.pitch, Rate)
  122.     local yaw = math.ApproachAngle( CurrentAng.yaw, TargetAng.yaw, Rate)
  123.     local roll = math.ApproachAngle( CurrentAng.roll, TargetAng.roll, Rate)
  124.  
  125.     return Angle(pitch, yaw, roll)
  126. end
RAW Paste Data