Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local AI = {}
- AI.Title = "Follow Player Car Keys"
- AI.Author = "Sakarias88"
- AI.IsState = true --If this is true it won't be added to the stool
- AI.Owner = nil -- This will be the entity that uses the AI
- ----------------- Variables
- AI.CollisionTimer = CurTime()
- AI.ColReverseTimer = CurTime()
- AI.whiskerHit = false
- AI.KeepTargetDistance = 400
- AI.Fails = 0
- function AI:Init() --Will only run once
- if self.Owner:IsConnectedToSCar() then
- local SCar = self.Owner:GetSCar()
- SCar:DoUnlockEffect()
- end
- end
- function AI:FastThink()
- end
- function AI:Think() --Runs 5 times per sec
- if self.Owner:IsConnectedToSCar() then
- local SCar = self.Owner:GetSCar()
- if self.CollisionTimer < CurTime() then
- if ValidEntity(self.Owner.SpawnedBy) then
- self.Owner:SetTarget( self.Owner.SpawnedBy )
- end
- if self.Owner:HasTarget() then
- local target = self.Owner:GetTarget()
- local dist = SCar:GetPos():Distance( target:GetPos() )
- local leftOrRight, frontOrRear = self.Owner:GetTargetSide()
- if dist > self.KeepTargetDistance then
- SCar:HandBrakeOff()
- local vel = SCar:GetPhysicsObject():GetVelocity():Length()
- if vel < 300 then
- if frontOrRear == 1 then
- SCar:GoForward()
- else
- SCar:GoBack()
- end
- elseif vel > 300 and vel < 400 then
- SCar:GoNeutral()
- else
- if frontOrRear == 1 then
- SCar:GoBack()
- else
- SCar:GoForward()
- end
- end
- self.Owner:UseTurning()
- else
- self:KillOperation( SCar )
- return
- end
- else
- SCar:GoNeutral()
- SCar:NotTurning()
- end
- else
- local SCar = self.Owner:GetSCar()
- if self.ColReverseTimer > CurTime() then
- SCar:GoBack()
- self.Owner:UseInvertedTurning()
- elseif self.CollisionTimer > CurTime() then
- SCar:GoForward()
- if self.whiskerHit then
- self.Owner:UseTurning()
- self.whiskerHit = false
- end
- end
- end
- if self.CollisionTimer < CurTime()&& self.Owner:HasTarget() && self.Owner:IsStuck() then
- self.CollisionTimer = CurTime() + 2
- self.ColReverseTimer = CurTime() + 0.7
- self.Fails = self.Fails + 1
- if self.Fails > 3 then
- self:KillOperation( SCar )
- return
- end
- end
- end
- end
- function AI:KillOperation( SCar )
- SCar:GoNeutral()
- SCar:NotTurning()
- SCar:HandBrakeOn()
- self.Owner:SetFace( "neutral" )
- self.Owner:Remove()
- timer.Create( "SCarSomeAiTimer"..SCar:EntIndex(), 1, 1, function()
- SCar:DoLockEffect()
- end )
- end
- function AI:OnTakeDamage(dmg)
- end
- --This is the PhysicsCollide( data, phys ) from the SCar but passed on here
- function AI:CarCollide( data, phys )
- end
- --Runs whenever a player enters the scar
- function AI:PlayerEnteredSCar( player )
- self:KillOperation( SCar )
- end
- --Whiskers
- function AI:LeftWhiskerHit()
- self.whiskerHit = true
- end
- function AI:RightWhiskerHit()
- self.whiskerHit = true
- end
- function AI:FrontWhiskerHit()
- end
- function AI:RearWhiskerHit()
- end
- SCarAiHandler:registerAI( AI )
- ---------------------AI help funcs
- --self.Owner:UseWhiskers( use ) --bool
- --When using MoveAwayFromPos(), GoToPositionition(), MoveToTarget(), MoveAwayFromTarget(), UseTurning() or UseInvertedTurning() the car
- --will automatically try to avoid obstacles and stay away from walls.
- --Off by default
- --self.Owner:SetWhiskerLength( length ) --Number
- --Sets the length of the whiskers
- --self.Owner:SetTarget( newTarget ) --Entity
- --Sets the target
- --self.Owner:SetAI( newAI ) --AI object
- --You can fetch AI objects from the SCarAiHandler.
- --Example: self.Owner:SetAI( SCarAiHandler.GetAiByName( "Follow Player" ) )
- --Will change the AI to the Follow Player AI.
- --It's fully possible to split up a bunch of AI's into states and then make the states automatically change to other states
- --and therefore keeping it very simple.
- --If you want to create an AI as a "state" you will have to set AI.IsState = true.
- --If AI.IsState == true it won't be added to the AI stool.
- --self.Owner:IsStuck()
- --Returns true when the AI thiks it's stuck.
- --Basically it will always return true if it have stayed still for too long.
- --self.Owner:HasTarget()
- --Returns true if the AI has a target
- --self.Owner:IsConnectedToSCar()
- --Returns true if the ai is connected to a scar
- --self.Owner:GetTarget()
- --Returns the target
- --self.Owner:GetSCar()
- --Returns the SCar
- --self.Owner:TargetIsNode()
- --Returns true if the target is a node
- --self.Owner:GetTargetSide()
- -- 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
- --Right side: First int = 1
- --Left side: First int = -1
- --Target is in the middle: First int = 0
- --Front: Second int = 1
- --Rear: Second int = -1
- --Target is in the middle of the right or left side: Second int = 0
- --self.Owner:UseTurning()
- -- Will turn left if the target is at the SCars left side and right if the target is at the SCars right side
- --self.Owner:UseInvertedTurning()
- -- Will turn right if the target is at the SCars left side and turn left if the target is at the SCars right side
- --self.Owner:GoToPosition( move , pos, distanceComplete ) -- bool, Vector, number
- --Makes the car go towards a position whitout obstacle avoidance.
- --The car will try to go to the position utill it's whithin the distanceComplete radius.
- --Setting move to false will interrupt this function.
- --Using MoveAwayFromPos(), MoveToTarget() or MoveAwayFromTarget() will interrupt this function.
- --MoveAwayFromPos( move, pos, distanceComplete ) --bool, vector, number
- --Makes the car move away from the position utill it's outside the distanceComplete radius.
- --Setting move to false will interrupt this function.
- --Using GoToPosition(), MoveToTarget() or MoveAwayFromTarget() will interrupt this function.
- --self.Owner:HasCompletedGoToPosition()
- --Will always return true unless the car is on it's way to a position
- --self.Owner:MoveToTarget( move, distanceComplete ) --bool, number
- --Makes the car go towards the target whitout obstacle avoidance.
- --Setting move to false will interrupt this function.
- --Using GoToPosition(), MoveAwayFromPos() or MoveAwayFromTarget() will interrupt this function.
- --MoveAwayFromTarget( move, distanceComplete ) --bool, number
- --Makes the car move away from the target utill it's outside the distanceComplete radius.
- --Setting move to false will interrupt this function.
- --Using GoToPosition(), MoveAwayFromPos() or MoveToTarget() will interrupt this function.
- --self.Owner:HasCompletedMoveToTarget()
- --Will always return true unless the car is on it's way to the target
- --self.Owner:AutoLookAtTarget( lookAt )
- --Is set to true by default.
- --If it's true it will automatically look at it's target if it has one.
- --If you want the AI to look at anything else than the target you should switch this one of
- --self.Owner:LookAtPosition( pos )
- --Will force it to look at a position even if AutoLookAtTarget is set to true
- ---------------------Useful SCar funcs
- --SCar:GoForward()
- --Makes the SCar go forward
- --SCar:GoBack()
- --Makes the car go backwards
- --SCar:GoNeutral()
- --No throttle
- --SCar:TurnLeft()
- --Turn left
- --SCar:TurnRight()
- --Turn right
- --SCar:NotTurning()
- --Return steering to norlam ( not turning left or right)
- --SCar:HandBrakeOn()
- --Hand brake on
- --SCar:HandBrakeOff()
- --Hand brake off
- --SCar:InitiateTurbo()
- --Start turbo
- --SCar:FrontLightsOn( col, alpha, rtOn )
- --This function takes 3 arguments. The two first ones are strings and the last one is a bool
- --Example: SCar:FrontLightsOn( "255 255 255" , "150" , true )
- --SCar:FrontLightsOff()
- --Turns front lights off
- --SCar:DoBurnout( force )
- --Make the car do a burnout. The force is the amount of force used to make the wheels spin.
- --SCar:ToggleHydraulics()
- --Toggle the car hydraulics on and off.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement