diff --git a/Embedded/Moose_Create_Embedded.bat b/Embedded/Moose_Create_Embedded.bat index af10f0daf..bd830b9a3 100644 --- a/Embedded/Moose_Create_Embedded.bat +++ b/Embedded/Moose_Create_Embedded.bat @@ -9,7 +9,8 @@ copy /b Moose_Embedded.lua + ..\Moose\Group.lua Moose_Embedded.l copy /b Moose_Embedded.lua + ..\Moose\Unit.lua Moose_Embedded.lua copy /b Moose_Embedded.lua + ..\Moose\Zone.lua Moose_Embedded.lua copy /b Moose_Embedded.lua + ..\Moose\Database.lua Moose_Embedded.lua -copy /b Moose_Embedded.lua + ..\Moose\Moose.lua Moose_Embedded.lua +copy /b Moose_Embedded.lua + ..\Moose\Moose.lua Moose_Embedded.lua +copy /b Moose_Embedded.lua + ..\Moose\TimeTrigger.lua Moose_Embedded.lua copy /b Moose_Embedded.lua + ..\Moose\Scoring.lua Moose_Embedded.lua copy /b Moose_Embedded.lua + ..\Moose\Cargo.lua Moose_Embedded.lua copy /b Moose_Embedded.lua + ..\Moose\Client.lua Moose_Embedded.lua @@ -31,3 +32,4 @@ copy /b Moose_Embedded.lua + ..\Moose\Spawn.lua Moose_Embedded.l copy /b Moose_Embedded.lua + ..\Moose\Movement.lua Moose_Embedded.lua copy /b Moose_Embedded.lua + ..\Moose\Sead.lua Moose_Embedded.lua copy /b Moose_Embedded.lua + ..\Moose\Escort.lua Moose_Embedded.lua +copy /b Moose_Embedded.lua + ..\Moose\MissileTrainer.lua Moose_Embedded.lua diff --git a/Embedded/Moose_Embedded.lua b/Embedded/Moose_Embedded.lua index a4bc4864c..459565c40 100644 --- a/Embedded/Moose_Embedded.lua +++ b/Embedded/Moose_Embedded.lua @@ -5900,6 +5900,90 @@ _EVENTDISPATCHER = EVENT:New() -- #EVENT --- Declare the main database object, which is used internally by the MOOSE classes. _DATABASE = DATABASE:New():ScanEnvironment() -- Database#DATABASE +--- 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 + + + + + + --- Scoring system for MOOSE. -- This scoring class calculates the hits and kills that players make within a simulation session. -- Scoring is calculated using a defined algorithm. @@ -14169,3 +14253,85 @@ function ESCORT:_ReportTargetsScheduler() self.ReportTargetsScheduler = nil end end +--- 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 diff --git a/Loaders/Moose_Create_Embedded.bat b/Loaders/Moose_Create_Embedded.bat deleted file mode 100644 index 4627a58d0..000000000 --- a/Loaders/Moose_Create_Embedded.bat +++ /dev/null @@ -1,31 +0,0 @@ -rem Generate Moose_Embedded.lua - -copy Trace.lua ^ - + Routines.lua ^ - + Base.lua ^ - + Menu.lua ^ - + Group.lua ^ - + Unit.lua ^ - + Zone.lua ^ - + Database.lua ^ - + Cargo.lua ^ - + Client.lua ^ - + Message.lua ^ - + Stage.lua ^ - + Task.lua ^ - + GoHomeTask.lua ^ - + DestroyBaseTask.lua ^ - + DestroyGroupsTask.lua ^ - + DestroyRadarsTask.lua ^ - + DestroyUnitTypesTask.lua ^ - + PickupTask.lua ^ - + DeployTask.lua ^ - + NoTask.lua ^ - + RouteTask.lua ^ - + Mission.lua ^ - + CleanUp.lua ^ - + Spawn.lua ^ - + Movement.lua ^ - + Sead.lua ^ - Moose_Embedded.lua - \ No newline at end of file diff --git a/Moose/MissileTrainer.lua b/Moose/MissileTrainer.lua new file mode 100644 index 000000000..cc2c15aa9 --- /dev/null +++ b/Moose/MissileTrainer.lua @@ -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 diff --git a/Moose/TimeTrigger.lua b/Moose/TimeTrigger.lua new file mode 100644 index 000000000..4b3a9f9a2 --- /dev/null +++ b/Moose/TimeTrigger.lua @@ -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 + + + + + + diff --git a/Test Missions/Moose_Test_MISSILETRAINER/Moose_Test_MISSILETRAINER.lua b/Test Missions/Moose_Test_MISSILETRAINER/Moose_Test_MISSILETRAINER.lua new file mode 100644 index 000000000..2aaabf1ff --- /dev/null +++ b/Test Missions/Moose_Test_MISSILETRAINER/Moose_Test_MISSILETRAINER.lua @@ -0,0 +1,6 @@ + + +Include.File("Moose") +Include.File("MissileTrainer") + +local Trainer = MISSILETRAINER:New( 200 ) \ No newline at end of file diff --git a/Test Missions/Moose_Test_MISSILETRAINER/Moose_Test_MISSILETRAINER.miz b/Test Missions/Moose_Test_MISSILETRAINER/Moose_Test_MISSILETRAINER.miz new file mode 100644 index 000000000..b1b93909d Binary files /dev/null and b/Test Missions/Moose_Test_MISSILETRAINER/Moose_Test_MISSILETRAINER.miz differ