MissileTrainer first version

This commit is contained in:
Sven Van de Velde
2016-05-03 13:47:02 +02:00
parent f8a5ebdadb
commit f981200409
7 changed files with 341 additions and 32 deletions

82
Moose/MissileTrainer.lua Normal file
View File

@@ -0,0 +1,82 @@
--- Provides missile training functions.
-- @module MissileTrainer
-- @author FlightControl
Include.File( "Client" )
Include.File( "TimeTrigger" )
--- The MISSILETRAINER class
-- @type MISSILETRAINER
-- @extends Base#BASE
MISSILETRAINER = {
ClassName = "MISSILETRAINER",
}
--- Creates the main object which is handling missile tracking.
-- When a missile is fired a TIMETRIGGER is set off that follows the missile. When near a certain a client player, the missile will be destroyed.
-- @param #MISSILETRAINER
-- @param #number Distance The distance in meters when a tracked missile needs to be destroyed when close to a player.
-- @return #MISSILETRAINER
function MISSILETRAINER:New( Distance )
local self = BASE:Inherit( self, BASE:New() )
self:F( Distance )
self.TimeTriggers = {}
self.TimeTriggerID = 0
self.Distance = Distance
_EVENTDISPATCHER:OnShot( self._EventShot, self )
return self
end
--- Detects if an SA site was shot with an anti radiation missile. In this case, take evasive actions based on the skill level set within the ME.
-- @see MISSILETRAINER
function MISSILETRAINER:_EventShot( Event )
self:F( { Event } )
local TrainerSourceDCSUnit = Event.IniDCSUnit
local TrainerSourceDCSUnitName = Event.IniDCSUnitName
local TrainerWeapon = Event.Weapon -- Identify the weapon fired
local TrainerWeaponName = Event.WeaponName -- return weapon type
self:T( "Missile Launched = " .. TrainerWeaponName )
local TrainerTargetDCSUnit = TrainerWeapon:getTarget() -- Identify target
local TrainerTargetDCSUnitName = Unit.getName( TrainerTargetDCSUnit )
local TrainerTargetDCSGroup = TrainerTargetDCSUnit:getGroup()
local TrainerTargetDCSGroupName = TrainerTargetDCSGroup:getName()
local TrainerTargetSkill = _DATABASE.Units[TrainerTargetDCSUnitName].Template.skill
self:T( TrainerTargetSkill )
if TrainerTargetSkill == "Client" or TrainerTargetSkill == "Player" then
self.TimeTriggers[#self.TimeTriggers+1] = TIMETRIGGER:New( self, self._FollowMissile, { TrainerSourceDCSUnit, TrainerWeapon, TrainerTargetDCSUnit }, 0.5, 0.05, 0 )
end
end
function MISSILETRAINER:_FollowMissile( TrainerSourceDCSUnit, TrainerWeapon, TrainerTargetDCSUnit )
self:F( { TrainerSourceDCSUnit, TrainerWeapon, TrainerTargetDCSUnit } )
local TrainerSourceUnit = UNIT:New( TrainerSourceDCSUnit )
local TrainerTargetUnit = UNIT:New( TrainerTargetDCSUnit )
local PositionMissile = TrainerWeapon:getPoint()
local PositionTarget = TrainerTargetUnit:GetPositionVec3()
local Distance = ( ( PositionMissile.x - PositionTarget.x )^2 +
( PositionMissile.y - PositionTarget.y )^2 +
( PositionMissile.z - PositionTarget.z )^2
) ^ 0.5
MESSAGE:New( "Distance Missle = " .. Distance, nil, 0.2, "/Missile" ):ToAll()
if Distance <= self.Distance then
TrainerWeapon:destroy()
MESSAGE:New( "Missle Destroyed", nil, 5, "/Missile" ):ToAll()
return false
end
return true
end

84
Moose/TimeTrigger.lua Normal file
View File

@@ -0,0 +1,84 @@
--- Models time events calling event handing functions.
-- @module TimeTrigger
-- @author FlightControl
Include.File( "Routines" )
Include.File( "Base" )
Include.File( "Cargo" )
Include.File( "Message" )
--- The TIMETRIGGER class
-- @type TIMETRIGGER
-- @extends Base#BASE
TIMETRIGGER = {
ClassName = "TIMETRIGGER",
}
--- TIMETRIGGER constructor.
-- @param #TIMETRIGGER self
-- @param #function TimeEventFunction
-- @param #table TimeEventFunctionArguments
-- @param #number StartSeconds
-- @param #number RepeatSecondsInterval
-- @param #number RandomizationFactor
-- @param #number StopSeconds
-- @return #TIMETRIGGER
function TIMETRIGGER:New( TimeEventObject, TimeEventFunction, TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds )
local self = BASE:Inherit( self, BASE:New() )
self:F( { TimeEventObject, TimeEventFunction, TimeEventFunctionArguments, StartSeconds, RepeatSecondsInterval, RandomizationFactor, StopSeconds } )
self.TimeEventObject = TimeEventObject
self.TimeEventFunction = TimeEventFunction
self.TimeEventFunctionArguments = TimeEventFunctionArguments
self.StartSeconds = StartSeconds
if RepeatSecondsInterval then
self.RepeatSecondsInterval = RepeatSecondsInterval
else
self.RepeatSecondsInterval = 0
end
if RandomizationFactor then
self.RandomizationFactor = RandomizationFactor
else
self.RandomizationFactor = 0
end
if StopSeconds then
self.StopSeconds = StopSeconds
end
self.StartTime = timer.getTime()
self:T("Calling function" .. timer.getTime() + self.StartSeconds )
timer.scheduleFunction( self.Scheduler, self, timer.getTime() + self.StartSeconds + .01 )
return self
end
function TIMETRIGGER:Scheduler()
self:F( self.TimeEventFunctionArguments )
local Result = self.TimeEventFunction( self.TimeEventObject, unpack( self.TimeEventFunctionArguments ) )
if Result and Result == true then
if not self.StopSeconds or ( self.StopSeconds and timer.getTime() <= self.StartTime + self.StopSeconds ) then
timer.scheduleFunction(
self.Scheduler,
self,
timer.getTime() + self.RepeatSecondsInterval * math.random( self.RandomizationFactor * self.RepeatSecondsInterval ) + 0.01
)
end
end
end