Advertisement
Guest User

Deathmatch-suited Gmod Bots

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