Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cruiseAltitude = 250
- diveDistance = 100
- -- Gain from 1 to 10
- rollGain = 5
- yawGain = 5
- pitchGain = 5
- minTargetAltitude = -5
- blueprintName = "SNUKEB"
- -- internal variables (do not touch)
- runOnce = true
- swarmId = 0
- function GetIntercept(I, Pos, Info)
- -- get my vehicle info
- local mySpeed = I:GetForwardsVelocityMagnitude()
- local myAltitude = I:GetConstructCenterOfMass().y
- --I:LogToHud(mySpeed .. " " Pos.Range)
- -- My attempt to make very basic prediction
- local Prediction = Info.AimPointPosition + Info.Velocity * ((Pos.Range / mySpeed) / 4)
- -- Aim straight after we reach "diveDistance"
- local adjustedDistance = math.max(0,Pos.GroundDistance - diveDistance)
- -- if we get above cruiseAltitude, lower the height aimpoint
- local height = Prediction.y + adjustedDistance / math.max(1, (myAltitude / cruiseAltitude) * 4)
- return I:GetTargetPositionInfoForPosition(0, Prediction.x, height, Prediction.z)
- end
- function NavigateToPoint(I, Pos)
- --Roll
- local roll = I:GetConstructRoll()
- if( roll >180 ) then roll = roll-360 end
- if(roll>0) then I:RequestControl(2,3,math.min(1,math.abs(roll)/(180/rollGain))) end -- roll right
- if(roll<0) then I:RequestControl(2,2,math.min(1,math.abs(roll)/(180/rollGain))) end -- roll left
- --Yaw
- if(Pos.Azimuth>0) then I:RequestControl(2,0,math.min(1,math.abs(Pos.Azimuth)/(180/yawGain))) end --yaw left
- if(Pos.Azimuth<0) then I:RequestControl(2,1,math.min(1,math.abs(Pos.Azimuth)/(180/yawGain))) end --yaw right
- --Pitch
- if(Pos.Elevation>0) then I:RequestControl(2,4,math.min(1,math.abs(Pos.Elevation)/(180/pitchGain))) end --elevate up
- if(Pos.Elevation<0) then I:RequestControl(2,5,math.min(1,math.abs(Pos.Elevation)/(180/pitchGain))) end --elevate down
- --I:LogToHud(Pos.Azimuth)
- end
- function SmartTargeting(I)
- -- swarmId is number of this craft in a swarm of similar crafts
- if(runOnce) then
- -- runOnce to false, so we dont run this again
- runOnce = false
- --Count how many similar units are in a 1km radius
- -- and assign myself a flight number
- local numFriendlies = I:GetFriendlyCount()
- for i = 0, numFriendlies-1, 1 do
- local friendlyInfo = I:GetFriendlyInfo(i)
- if(friendlyInfo.BlueprintName == blueprintName) then
- swarmId = swarmId+1
- end
- end
- I:Log("Set swarmId: " .. swarmId)
- end
- --Count number of possilbe targets we could engage
- local numTargets= I:GetNumberOfTargets(0)
- local numValidTargets= 0
- for i = 0, numTargets-1, 1 do
- local TargetInfo = I:GetTargetInfo(0,i)
- if(TargetInfo.Valid and (TargetInfo.Position.y > minTargetAltitude)) then
- numValidTargets= numValidTargets+1
- end
- end
- --return a target number
- local myTarget = 0
- if( numValidTargets > 0)then
- myTarget= swarmId%numValidTargets
- if (myTarget == 0 ) then
- myTarget = numValidTargets
- end
- myTarget = myTarget-1
- end
- I:LogToHud(blueprintName .. " swarmId: " .. swarmId .. " targeting: " .. myTarget)
- return myTarget
- end
- function Update(I)
- local myTarget = SmartTargeting(I)
- local TargetPos = I:GetTargetPositionInfo(0, myTarget )
- local TargetInfo = I:GetTargetInfo(0, myTarget)
- if (I:GetNumberOfMainframes() > 0 and I:GetNumberOfTargets(0) > 0) then
- if TargetPos.Valid then
- local intercept = GetIntercept(I, TargetPos, TargetInfo )
- NavigateToPoint(I, intercept)
- --thrust full speed
- I:RequestThrustControl(0)
- end
- else
- local myPos = I:GetConstructCenterOfMass()
- local intercept =I:GetTargetPositionInfoForPosition(0,
- myPos.x+I:GetConstructRightVector().x*50,
- cruiseAltitude,
- myPos.z+I:GetConstructRightVector().z*50)
- NavigateToPoint(I, intercept)
- --thrust low speed
- I:RequestThrustControl(0,0.25)
- end
- -- self destruct NOTE: does not work as of 1.966 -- commented out
- -- since NUKE is not a weapon-type and I:GetWeaponCount() returns 0
- -- if((I:GetVelocityMagnitude()<25 and TargetPos.Valid and TargetPos.Range<100)) then
- -- local aim = I:GetConstructForwardVector()
- -- I:AimWeaponInDirection(0, aim.x,aim.y,aim.z, 0)
- -- I:FireWeapon(0,0)
- -- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement