Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1.  
  2. local AI = {}
  3. AI.Title = "Follow Player Car Keys"
  4. AI.Author = "Sakarias88"
  5. AI.IsState = true --If this is true it won't be added to the stool
  6.  
  7. AI.Owner = nil -- This will be the entity that uses the AI
  8.  
  9. ----------------- Variables
  10. AI.CollisionTimer = CurTime()
  11. AI.ColReverseTimer = CurTime()
  12. AI.whiskerHit = false
  13.  
  14. AI.KeepTargetDistance = 400
  15. AI.Fails = 0
  16.  
  17. function AI:Init() --Will only run once
  18. if self.Owner:IsConnectedToSCar() then
  19. local SCar = self.Owner:GetSCar()
  20. SCar:DoUnlockEffect()
  21. end
  22. end
  23.  
  24. function AI:FastThink()
  25. end
  26.  
  27.  
  28. function AI:Think() --Runs 5 times per sec
  29.  
  30. if self.Owner:IsConnectedToSCar() then
  31.  
  32. local SCar = self.Owner:GetSCar()
  33. if self.CollisionTimer < CurTime() then
  34.  
  35.  
  36. if ValidEntity(self.Owner.SpawnedBy) then
  37. self.Owner:SetTarget( self.Owner.SpawnedBy )
  38. end
  39.  
  40.  
  41.  
  42. if self.Owner:HasTarget() then
  43.  
  44. local target = self.Owner:GetTarget()
  45. local dist = SCar:GetPos():Distance( target:GetPos() )
  46.  
  47. local leftOrRight, frontOrRear = self.Owner:GetTargetSide()
  48.  
  49. if dist > self.KeepTargetDistance then
  50. SCar:HandBrakeOff()
  51.  
  52. local vel = SCar:GetPhysicsObject():GetVelocity():Length()
  53.  
  54. if vel < 300 then
  55. if frontOrRear == 1 then
  56. SCar:GoForward()
  57. else
  58. SCar:GoBack()
  59. end
  60. elseif vel > 300 and vel < 400 then
  61. SCar:GoNeutral()
  62. else
  63. if frontOrRear == 1 then
  64. SCar:GoBack()
  65. else
  66. SCar:GoForward()
  67. end
  68. end
  69.  
  70.  
  71.  
  72. self.Owner:UseTurning()
  73. else
  74. self:KillOperation( SCar )
  75. return
  76. end
  77. else
  78. SCar:GoNeutral()
  79. SCar:NotTurning()
  80. end
  81.  
  82.  
  83. else
  84. local SCar = self.Owner:GetSCar()
  85.  
  86. if self.ColReverseTimer > CurTime() then
  87. SCar:GoBack()
  88.  
  89. self.Owner:UseInvertedTurning()
  90. elseif self.CollisionTimer > CurTime() then
  91. SCar:GoForward()
  92.  
  93. if self.whiskerHit then
  94. self.Owner:UseTurning()
  95. self.whiskerHit = false
  96. end
  97. end
  98. end
  99.  
  100.  
  101. if self.CollisionTimer < CurTime()&& self.Owner:HasTarget() && self.Owner:IsStuck() then
  102. self.CollisionTimer = CurTime() + 2
  103. self.ColReverseTimer = CurTime() + 0.7
  104. self.Fails = self.Fails + 1
  105.  
  106. if self.Fails > 3 then
  107. self:KillOperation( SCar )
  108. return
  109. end
  110.  
  111. end
  112. end
  113. end
  114.  
  115. function AI:KillOperation( SCar )
  116. SCar:GoNeutral()
  117. SCar:NotTurning()
  118. SCar:HandBrakeOn()
  119. self.Owner:SetFace( "neutral" )
  120. self.Owner:Remove()
  121.  
  122. timer.Create( "SCarSomeAiTimer"..SCar:EntIndex(), 1, 1, function()
  123. SCar:DoLockEffect()
  124. end )
  125. end
  126.  
  127. function AI:OnTakeDamage(dmg)
  128. end
  129.  
  130. --This is the PhysicsCollide( data, phys ) from the SCar but passed on here
  131. function AI:CarCollide( data, phys )
  132. end
  133.  
  134. --Runs whenever a player enters the scar
  135. function AI:PlayerEnteredSCar( player )
  136. self:KillOperation( SCar )
  137. end
  138.  
  139. --Whiskers
  140. function AI:LeftWhiskerHit()
  141. self.whiskerHit = true
  142. end
  143.  
  144. function AI:RightWhiskerHit()
  145. self.whiskerHit = true
  146. end
  147.  
  148. function AI:FrontWhiskerHit()
  149.  
  150. end
  151.  
  152. function AI:RearWhiskerHit()
  153.  
  154. end
  155.  
  156.  
  157.  
  158. SCarAiHandler:registerAI( AI )
  159.  
  160. ---------------------AI help funcs
  161.  
  162. --self.Owner:UseWhiskers( use ) --bool
  163. --When using MoveAwayFromPos(), GoToPositionition(), MoveToTarget(), MoveAwayFromTarget(), UseTurning() or UseInvertedTurning() the car
  164. --will automatically try to avoid obstacles and stay away from walls.
  165. --Off by default
  166.  
  167.  
  168. --self.Owner:SetWhiskerLength( length ) --Number
  169. --Sets the length of the whiskers
  170.  
  171.  
  172. --self.Owner:SetTarget( newTarget ) --Entity
  173. --Sets the target
  174.  
  175.  
  176. --self.Owner:SetAI( newAI ) --AI object
  177. --You can fetch AI objects from the SCarAiHandler.
  178. --Example: self.Owner:SetAI( SCarAiHandler.GetAiByName( "Follow Player" ) )
  179. --Will change the AI to the Follow Player AI.
  180. --It's fully possible to split up a bunch of AI's into states and then make the states automatically change to other states
  181. --and therefore keeping it very simple.
  182. --If you want to create an AI as a "state" you will have to set AI.IsState = true.
  183. --If AI.IsState == true it won't be added to the AI stool.
  184.  
  185.  
  186. --self.Owner:IsStuck()
  187. --Returns true when the AI thiks it's stuck.
  188. --Basically it will always return true if it have stayed still for too long.
  189.  
  190.  
  191. --self.Owner:HasTarget()
  192. --Returns true if the AI has a target
  193.  
  194.  
  195. --self.Owner:IsConnectedToSCar()
  196. --Returns true if the ai is connected to a scar
  197.  
  198.  
  199. --self.Owner:GetTarget()
  200. --Returns the target
  201.  
  202.  
  203. --self.Owner:GetSCar()
  204. --Returns the SCar
  205.  
  206.  
  207. --self.Owner:TargetIsNode()
  208. --Returns true if the target is a node
  209.  
  210.  
  211. --self.Owner:GetTargetSide()
  212. -- Returns two integers. The first one is which side of the car the target is located and the second integer says if it's in front or behind the SCar
  213. --Right side: First int = 1
  214. --Left side: First int = -1
  215. --Target is in the middle: First int = 0
  216. --Front: Second int = 1
  217. --Rear: Second int = -1
  218. --Target is in the middle of the right or left side: Second int = 0
  219.  
  220.  
  221. --self.Owner:UseTurning()
  222. -- Will turn left if the target is at the SCars left side and right if the target is at the SCars right side
  223.  
  224.  
  225. --self.Owner:UseInvertedTurning()
  226. -- Will turn right if the target is at the SCars left side and turn left if the target is at the SCars right side
  227.  
  228.  
  229. --self.Owner:GoToPosition( move , pos, distanceComplete ) -- bool, Vector, number
  230. --Makes the car go towards a position whitout obstacle avoidance.
  231. --The car will try to go to the position utill it's whithin the distanceComplete radius.
  232. --Setting move to false will interrupt this function.
  233. --Using MoveAwayFromPos(), MoveToTarget() or MoveAwayFromTarget() will interrupt this function.
  234.  
  235.  
  236. --MoveAwayFromPos( move, pos, distanceComplete ) --bool, vector, number
  237. --Makes the car move away from the position utill it's outside the distanceComplete radius.
  238. --Setting move to false will interrupt this function.
  239. --Using GoToPosition(), MoveToTarget() or MoveAwayFromTarget() will interrupt this function.
  240.  
  241.  
  242. --self.Owner:HasCompletedGoToPosition()
  243. --Will always return true unless the car is on it's way to a position
  244.  
  245.  
  246. --self.Owner:MoveToTarget( move, distanceComplete ) --bool, number
  247. --Makes the car go towards the target whitout obstacle avoidance.
  248. --Setting move to false will interrupt this function.
  249. --Using GoToPosition(), MoveAwayFromPos() or MoveAwayFromTarget() will interrupt this function.
  250.  
  251.  
  252. --MoveAwayFromTarget( move, distanceComplete ) --bool, number
  253. --Makes the car move away from the target utill it's outside the distanceComplete radius.
  254. --Setting move to false will interrupt this function.
  255. --Using GoToPosition(), MoveAwayFromPos() or MoveToTarget() will interrupt this function.
  256.  
  257. --self.Owner:HasCompletedMoveToTarget()
  258. --Will always return true unless the car is on it's way to the target
  259.  
  260. --self.Owner:AutoLookAtTarget( lookAt )
  261. --Is set to true by default.
  262. --If it's true it will automatically look at it's target if it has one.
  263. --If you want the AI to look at anything else than the target you should switch this one of
  264.  
  265. --self.Owner:LookAtPosition( pos )
  266. --Will force it to look at a position even if AutoLookAtTarget is set to true
  267.  
  268. ---------------------Useful SCar funcs
  269. --SCar:GoForward()
  270. --Makes the SCar go forward
  271.  
  272. --SCar:GoBack()
  273. --Makes the car go backwards
  274.  
  275. --SCar:GoNeutral()
  276. --No throttle
  277.  
  278. --SCar:TurnLeft()
  279. --Turn left
  280.  
  281. --SCar:TurnRight()
  282. --Turn right
  283.  
  284. --SCar:NotTurning()
  285. --Return steering to norlam ( not turning left or right)
  286.  
  287. --SCar:HandBrakeOn()
  288. --Hand brake on
  289.  
  290. --SCar:HandBrakeOff()
  291. --Hand brake off
  292.  
  293. --SCar:InitiateTurbo()
  294. --Start turbo
  295.  
  296. --SCar:FrontLightsOn( col, alpha, rtOn )
  297. --This function takes 3 arguments. The two first ones are strings and the last one is a bool
  298. --Example: SCar:FrontLightsOn( "255 255 255" , "150" , true )
  299.  
  300. --SCar:FrontLightsOff()
  301. --Turns front lights off
  302.  
  303. --SCar:DoBurnout( force )
  304. --Make the car do a burnout. The force is the amount of force used to make the wheels spin.
  305.  
  306. --SCar:ToggleHydraulics()
  307. --Toggle the car hydraulics on and off.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement