diff --git a/Moose Development/Moose/Core/Database.lua b/Moose Development/Moose/Core/Database.lua index e634bba13..3b89132fc 100644 --- a/Moose Development/Moose/Core/Database.lua +++ b/Moose Development/Moose/Core/Database.lua @@ -1,9 +1,9 @@ ---- **Core** - Manages several databases containing templates, mission objects, and mission information. --- +--- **Core** - Manages several databases containing templates, mission objects, and mission information. +-- -- === --- +-- -- ## Features: --- +-- -- * During mission startup, scan the mission environment, and create / instantiate intelligently the different objects as defined within the mission. -- * Manage database of DCS Group templates (as modelled using the mission editor). -- - Group templates. @@ -20,14 +20,14 @@ -- * Manage database of hits to units and statics. -- * Manage database of destroys of units and statics. -- * Manage database of @{Core.Zone#ZONE_BASE} objects. --- +-- -- === --- +-- -- ### Author: **FlightControl** --- ### Contributions: --- +-- ### Contributions: +-- -- === --- +-- -- @module Core.Database -- @image Core_Database.JPG @@ -36,9 +36,9 @@ -- @extends Core.Base#BASE --- Contains collections of wrapper objects defined within MOOSE that reflect objects within the simulator. --- +-- -- Mission designers can use the DATABASE class to refer to: --- +-- -- * STATICS -- * UNITS -- * GROUPS @@ -47,12 +47,12 @@ -- * PLAYERSJOINED -- * PLAYERS -- * CARGOS --- +-- -- On top, for internal MOOSE administration purposes, the DATBASE administers the Unit and Group TEMPLATES as defined within the Mission Editor. --- +-- -- The singleton object **_DATABASE** is automatically created by MOOSE, that administers all objects within the mission. -- Moose refers to **_DATABASE** within the framework extensively, but you can also refer to the _DATABASE object within your missions if required. --- +-- -- @field #DATABASE DATABASE = { ClassName = "DATABASE", @@ -116,7 +116,7 @@ function DATABASE:New() local self = BASE:Inherit( self, BASE:New() ) -- #DATABASE self:SetEventPriority( 1 ) - + self:HandleEvent( EVENTS.Birth, self._EventOnBirth ) self:HandleEvent( EVENTS.Dead, self._EventOnDeadOrCrash ) self:HandleEvent( EVENTS.Crash, self._EventOnDeadOrCrash ) @@ -126,11 +126,11 @@ function DATABASE:New() self:HandleEvent( EVENTS.DeleteCargo ) self:HandleEvent( EVENTS.NewZone ) self:HandleEvent( EVENTS.DeleteZone ) - + -- Follow alive players and clients --self:HandleEvent( EVENTS.PlayerEnterUnit, self._EventOnPlayerEnterUnit ) -- This is not working anymore!, handling this through the birth event. self:HandleEvent( EVENTS.PlayerLeaveUnit, self._EventOnPlayerLeaveUnit ) - + self:_RegisterTemplates() self:_RegisterGroupsAndUnits() self:_RegisterClients() @@ -139,16 +139,16 @@ function DATABASE:New() self:_RegisterAirbases() self.UNITS_Position = 0 - + --- @param #DATABASE self local function CheckPlayers( self ) - + local CoalitionsData = { AlivePlayersRed = coalition.getPlayers( coalition.side.RED ), AlivePlayersBlue = coalition.getPlayers( coalition.side.BLUE ), AlivePlayersNeutral = coalition.getPlayers( coalition.side.NEUTRAL )} for CoalitionId, CoalitionData in pairs( CoalitionsData ) do --self:E( { "CoalitionData:", CoalitionData } ) for UnitId, UnitData in pairs( CoalitionData ) do if UnitData and UnitData:isExist() then - + local UnitName = UnitData:getName() local PlayerName = UnitData:getPlayerName() local PlayerUnit = UNIT:Find( UnitData ) @@ -167,10 +167,10 @@ function DATABASE:New() end end end - + --self:E( "Scheduling" ) --PlayerCheckSchedule = SCHEDULER:New( nil, CheckPlayers, { self }, 1, 1 ) - + return self end @@ -193,10 +193,10 @@ function DATABASE:AddUnit( DCSUnitName ) self:T( { "Add UNIT:", DCSUnitName } ) local UnitRegister = UNIT:Register( DCSUnitName ) self.UNITS[DCSUnitName] = UNIT:Register( DCSUnitName ) - + table.insert( self.UNITS_Index, DCSUnitName ) end - + return self.UNITS[DCSUnitName] end @@ -205,7 +205,7 @@ end -- @param #DATABASE self function DATABASE:DeleteUnit( DCSUnitName ) - self.UNITS[DCSUnitName] = nil + self.UNITS[DCSUnitName] = nil end --- Adds a Static based on the Static Name in the DATABASE. @@ -216,7 +216,7 @@ function DATABASE:AddStatic( DCSStaticName ) self.STATICS[DCSStaticName] = STATIC:Register( DCSStaticName ) return self.STATICS[DCSStaticName] end - + return nil end @@ -225,7 +225,7 @@ end -- @param #DATABASE self function DATABASE:DeleteStatic( DCSStaticName ) - --self.STATICS[DCSStaticName] = nil + --self.STATICS[DCSStaticName] = nil end --- Finds a STATIC based on the StaticName. @@ -257,7 +257,7 @@ function DATABASE:AddAirbase( AirbaseName ) if not self.AIRBASES[AirbaseName] then self.AIRBASES[AirbaseName] = AIRBASE:Register( AirbaseName ) end - + return self.AIRBASES[AirbaseName] end @@ -267,7 +267,7 @@ end -- @param #string AirbaseName The name of the airbase function DATABASE:DeleteAirbase( AirbaseName ) - self.AIRBASES[AirbaseName] = nil + self.AIRBASES[AirbaseName] = nil end --- Finds an AIRBASE based on the AirbaseName. @@ -288,29 +288,29 @@ do -- Zones -- @param #string ZoneName The name of the zone. -- @return Core.Zone#ZONE_BASE The found ZONE. function DATABASE:FindZone( ZoneName ) - + local ZoneFound = self.ZONES[ZoneName] return ZoneFound end - + --- Adds a @{Zone} based on the zone name in the DATABASE. -- @param #DATABASE self -- @param #string ZoneName The name of the zone. -- @param Core.Zone#ZONE_BASE Zone The zone. function DATABASE:AddZone( ZoneName, Zone ) - + if not self.ZONES[ZoneName] then self.ZONES[ZoneName] = Zone end end - - + + --- Deletes a @{Zone} from the DATABASE based on the zone name. -- @param #DATABASE self -- @param #string ZoneName The name of the zone. function DATABASE:DeleteZone( ZoneName ) - - self.ZONES[ZoneName] = nil + + self.ZONES[ZoneName] = nil end @@ -327,20 +327,20 @@ do -- Zones self.ZONENAMES[ZoneName] = ZoneName self:AddZone( ZoneName, Zone ) end - + for ZoneGroupName, ZoneGroup in pairs( self.GROUPS ) do if ZoneGroupName:match("#ZONE_POLYGON") then local ZoneName1 = ZoneGroupName:match("(.*)#ZONE_POLYGON") local ZoneName2 = ZoneGroupName:match(".*#ZONE_POLYGON(.*)") local ZoneName = ZoneName1 .. ( ZoneName2 or "" ) - + self:I( { "Register ZONE_POLYGON:", Name = ZoneName } ) local Zone_Polygon = ZONE_POLYGON:New( ZoneName, ZoneGroup ) self.ZONENAMES[ZoneName] = ZoneName self:AddZone( ZoneName, Zone_Polygon ) end end - + end @@ -353,29 +353,29 @@ do -- Zone_Goal -- @param #string ZoneName The name of the zone. -- @return Core.Zone#ZONE_BASE The found ZONE. function DATABASE:FindZoneGoal( ZoneName ) - + local ZoneFound = self.ZONES_GOAL[ZoneName] return ZoneFound end - + --- Adds a @{Zone} based on the zone name in the DATABASE. -- @param #DATABASE self -- @param #string ZoneName The name of the zone. -- @param Core.Zone#ZONE_BASE Zone The zone. function DATABASE:AddZoneGoal( ZoneName, Zone ) - + if not self.ZONES_GOAL[ZoneName] then self.ZONES_GOAL[ZoneName] = Zone end end - - + + --- Deletes a @{Zone} from the DATABASE based on the zone name. -- @param #DATABASE self -- @param #string ZoneName The name of the zone. function DATABASE:DeleteZoneGoal( ZoneName ) - - self.ZONES_GOAL[ZoneName] = nil + + self.ZONES_GOAL[ZoneName] = nil end end -- Zone_Goal @@ -385,31 +385,31 @@ do -- cargo -- @param #DATABASE self -- @param #string CargoName The name of the airbase function DATABASE:AddCargo( Cargo ) - + if not self.CARGOS[Cargo.Name] then self.CARGOS[Cargo.Name] = Cargo end end - - + + --- Deletes a Cargo from the DATABASE based on the Cargo Name. -- @param #DATABASE self -- @param #string CargoName The name of the airbase function DATABASE:DeleteCargo( CargoName ) - - self.CARGOS[CargoName] = nil + + self.CARGOS[CargoName] = nil end - + --- Finds an CARGO based on the CargoName. -- @param #DATABASE self -- @param #string CargoName -- @return Wrapper.Cargo#CARGO The found CARGO. function DATABASE:FindCargo( CargoName ) - + local CargoFound = self.CARGOS[CargoName] return CargoFound end - + --- Checks if the Template name has a #CARGO tag. -- If yes, the group is a cargo. -- @param #DATABASE self @@ -418,10 +418,10 @@ do -- cargo function DATABASE:IsCargo( TemplateName ) TemplateName = env.getValueDictByKey( TemplateName ) - + local Cargo = TemplateName:match( "#(CARGO)" ) - return Cargo and Cargo == "CARGO" + return Cargo and Cargo == "CARGO" end --- Private method that registers new Static Templates within the DATABASE Object. @@ -430,7 +430,7 @@ do -- cargo function DATABASE:_RegisterCargos() local Groups = UTILS.DeepCopy( self.GROUPS ) -- This is a very important statement. CARGO_GROUP:New creates a new _DATABASE.GROUP entry, which will confuse the loop. I searched 4 hours on this to find the bug! - + for CargoGroupName, CargoGroup in pairs( Groups ) do self:I( { Cargo = CargoGroupName } ) if self:IsCargo( CargoGroupName ) then @@ -443,12 +443,12 @@ do -- cargo local Name = CargoParam and CargoParam:match( "N=([%a%d]+),?") or CargoName local LoadRadius = CargoParam and tonumber( CargoParam:match( "RR=([%a%d]+),?") ) local NearRadius = CargoParam and tonumber( CargoParam:match( "NR=([%a%d]+),?") ) - + self:I({"Register CargoGroup:",Type=Type,Name=Name,LoadRadius=LoadRadius,NearRadius=NearRadius}) CARGO_GROUP:New( CargoGroup, Type, Name, LoadRadius, NearRadius ) end end - + for CargoStaticName, CargoStatic in pairs( self.STATICS ) do if self:IsCargo( CargoStaticName ) then local CargoInfo = CargoStaticName:match("#CARGO(.*)") @@ -459,7 +459,7 @@ do -- cargo local Name = CargoParam and CargoParam:match( "N=([%a%d]+),?") or CargoName local LoadRadius = CargoParam and tonumber( CargoParam:match( "RR=([%a%d]+),?") ) local NearRadius = CargoParam and tonumber( CargoParam:match( "NR=([%a%d]+),?") ) - + if Category == "SLING" then self:I({"Register CargoSlingload:",Type=Type,Name=Name,LoadRadius=LoadRadius,NearRadius=NearRadius}) CARGO_SLINGLOAD:New( CargoStatic, Type, Name, LoadRadius, NearRadius ) @@ -471,7 +471,7 @@ do -- cargo end end end - + end end -- cargo @@ -517,9 +517,9 @@ function DATABASE:AddGroup( GroupName ) if not self.GROUPS[GroupName] then self:T( { "Add GROUP:", GroupName } ) self.GROUPS[GroupName] = GROUP:Register( GroupName ) - end - - return self.GROUPS[GroupName] + end + + return self.GROUPS[GroupName] end --- Adds a player based on the Player Name in the DATABASE. @@ -621,7 +621,7 @@ function DATABASE:Spawn( SpawnTemplate ) for UnitID, UnitData in pairs( SpawnTemplate.units ) do self:AddUnit( UnitData.name ) end - + return SpawnGroup end @@ -653,21 +653,21 @@ end function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionSide, CategoryID, CountryID, GroupName ) local GroupTemplateName = GroupName or env.getValueDictByKey( GroupTemplate.name ) - + if not self.Templates.Groups[GroupTemplateName] then self.Templates.Groups[GroupTemplateName] = {} self.Templates.Groups[GroupTemplateName].Status = nil end - + -- Delete the spans from the route, it is not needed and takes memory. - if GroupTemplate.route and GroupTemplate.route.spans then + if GroupTemplate.route and GroupTemplate.route.spans then GroupTemplate.route.spans = nil end - + GroupTemplate.CategoryID = CategoryID GroupTemplate.CoalitionID = CoalitionSide GroupTemplate.CountryID = CountryID - + self.Templates.Groups[GroupTemplateName].GroupName = GroupTemplateName self.Templates.Groups[GroupTemplateName].Template = GroupTemplate self.Templates.Groups[GroupTemplateName].groupId = GroupTemplate.groupId @@ -682,7 +682,7 @@ function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionSide, Category for unit_num, UnitTemplate in pairs( GroupTemplate.units ) do UnitTemplate.name = env.getValueDictByKey(UnitTemplate.name) - + self.Templates.Units[UnitTemplate.name] = {} self.Templates.Units[UnitTemplate.name].UnitName = UnitTemplate.name self.Templates.Units[UnitTemplate.name].Template = UnitTemplate @@ -700,8 +700,8 @@ function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionSide, Category self.Templates.ClientsByName[UnitTemplate.name].CountryID = CountryID self.Templates.ClientsByID[UnitTemplate.unitId] = UnitTemplate end - - UnitNames[#UnitNames+1] = self.Templates.Units[UnitTemplate.name].UnitName + + UnitNames[#UnitNames+1] = self.Templates.Units[UnitTemplate.name].UnitName end self:T( { Group = self.Templates.Groups[GroupTemplateName].GroupName, @@ -730,13 +730,13 @@ function DATABASE:_RegisterStaticTemplate( StaticTemplate, CoalitionID, Category local StaticTemplate = UTILS.DeepCopy( StaticTemplate ) local StaticTemplateName = env.getValueDictByKey(StaticTemplate.name) - + self.Templates.Statics[StaticTemplateName] = self.Templates.Statics[StaticTemplateName] or {} - + StaticTemplate.CategoryID = CategoryID StaticTemplate.CoalitionID = CoalitionID StaticTemplate.CountryID = CountryID - + self.Templates.Statics[StaticTemplateName].StaticName = StaticTemplateName self.Templates.Statics[StaticTemplateName].GroupTemplate = StaticTemplate self.Templates.Statics[StaticTemplateName].UnitTemplate = StaticTemplate.units[1] @@ -747,12 +747,12 @@ function DATABASE:_RegisterStaticTemplate( StaticTemplate, CoalitionID, Category self:I( { Static = self.Templates.Statics[StaticTemplateName].StaticName, Coalition = self.Templates.Statics[StaticTemplateName].CoalitionID, Category = self.Templates.Statics[StaticTemplateName].CategoryID, - Country = self.Templates.Statics[StaticTemplateName].CountryID + Country = self.Templates.Statics[StaticTemplateName].CountryID } ) - + self:AddStatic( StaticTemplateName ) - + end @@ -820,7 +820,7 @@ function DATABASE:_RegisterPlayers() end end end - + return self end @@ -836,12 +836,12 @@ function DATABASE:_RegisterGroupsAndUnits() if DCSGroup:isExist() then local DCSGroupName = DCSGroup:getName() - + self:I( { "Register Group:", DCSGroupName } ) self:AddGroup( DCSGroupName ) for DCSUnitId, DCSUnit in pairs( DCSGroup:getUnits() ) do - + local DCSUnitName = DCSUnit:getName() self:I( { "Register Unit:", DCSUnitName } ) self:AddUnit( DCSUnitName ) @@ -849,10 +849,10 @@ function DATABASE:_RegisterGroupsAndUnits() else self:E( { "Group does not exist: ", DCSGroup } ) end - + end end - + self:T("Groups:") for GroupName, Group in pairs( self.GROUPS ) do self:T( { "Group:", GroupName } ) @@ -870,7 +870,7 @@ function DATABASE:_RegisterClients() self:T( { "Register Client:", ClientName } ) self:AddClient( ClientName ) end - + return self end @@ -884,7 +884,7 @@ function DATABASE:_RegisterStatics() if DCSStatic:isExist() then local DCSStaticName = DCSStatic:getName() - + self:T( { "Register Static:", DCSStaticName } ) self:AddStatic( DCSStaticName ) else @@ -914,14 +914,14 @@ function DATABASE:_RegisterAirbases() for DCSAirbaseId, DCSAirbase in pairs(world.getAirbases()) do local DCSAirbaseName = DCSAirbase:getName() - + -- This gives the incorrect value to be inserted into the airdromeID for DCS 2.5.6! local airbaseID=DCSAirbase:getID() - + local airbase=self:AddAirbase( DCSAirbaseName ) - - self:I(string.format("Register Airbase: %s, getID=%d, GetID=%d (unique=%d)", DCSAirbaseName, DCSAirbase:getID(), airbase:GetID(), airbase:GetID(true))) - end + + self:I(string.format("Register Airbase: %s, getID=%d, GetID=%d (unique=%d)", DCSAirbaseName, DCSAirbase:getID(), airbase:GetID(), airbase:GetID(true))) + end return self end @@ -937,7 +937,7 @@ function DATABASE:_EventOnBirth( Event ) if Event.IniDCSUnit then if Event.IniObjectCategory == 3 then - self:AddStatic( Event.IniDCSUnitName ) + self:AddStatic( Event.IniDCSUnitName ) else if Event.IniObjectCategory == 1 then self:AddUnit( Event.IniDCSUnitName ) @@ -979,7 +979,7 @@ function DATABASE:_EventOnDeadOrCrash( Event ) if Event.IniObjectCategory == 3 then if self.STATICS[Event.IniDCSUnitName] then self:DeleteStatic( Event.IniDCSUnitName ) - end + end else if Event.IniObjectCategory == 1 then if self.UNITS[Event.IniDCSUnitName] then @@ -988,7 +988,7 @@ function DATABASE:_EventOnDeadOrCrash( Event ) end end end - + self:AccountDestroys( Event ) end @@ -1042,7 +1042,7 @@ end -- @return #DATABASE self function DATABASE:ForEach( IteratorFunction, FinalizeFunction, arg, Set ) self:F2( arg ) - + local function CoRoutine() local Count = 0 for ObjectID, Object in pairs( Set ) do @@ -1051,20 +1051,20 @@ function DATABASE:ForEach( IteratorFunction, FinalizeFunction, arg, Set ) Count = Count + 1 -- if Count % 100 == 0 then -- coroutine.yield( false ) --- end +-- end end return true end - + -- local co = coroutine.create( CoRoutine ) local co = CoRoutine - + local function Schedule() - + -- local status, res = coroutine.resume( co ) local status, res = co() self:T3( { status, res } ) - + if status == false then error( res ) end @@ -1079,7 +1079,7 @@ function DATABASE:ForEach( IteratorFunction, FinalizeFunction, arg, Set ) --local Scheduler = SCHEDULER:New( self, Schedule, {}, 0.001, 0.001, 0 ) Schedule() - + return self end @@ -1090,7 +1090,7 @@ end -- @return #DATABASE self function DATABASE:ForEachStatic( IteratorFunction, FinalizeFunction, ... ) --R2.1 self:F2( arg ) - + self:ForEach( IteratorFunction, FinalizeFunction, arg, self.STATICS ) return self @@ -1103,7 +1103,7 @@ end -- @return #DATABASE self function DATABASE:ForEachUnit( IteratorFunction, FinalizeFunction, ... ) self:F2( arg ) - + self:ForEach( IteratorFunction, FinalizeFunction, arg, self.UNITS ) return self @@ -1116,7 +1116,7 @@ end -- @return #DATABASE self function DATABASE:ForEachGroup( IteratorFunction, FinalizeFunction, ... ) self:F2( arg ) - + self:ForEach( IteratorFunction, FinalizeFunction, arg, self.GROUPS ) return self @@ -1129,9 +1129,9 @@ end -- @return #DATABASE self function DATABASE:ForEachPlayer( IteratorFunction, FinalizeFunction, ... ) self:F2( arg ) - + self:ForEach( IteratorFunction, FinalizeFunction, arg, self.PLAYERS ) - + return self end @@ -1142,9 +1142,9 @@ end -- @return #DATABASE self function DATABASE:ForEachPlayerJoined( IteratorFunction, FinalizeFunction, ... ) self:F2( arg ) - + self:ForEach( IteratorFunction, FinalizeFunction, arg, self.PLAYERSJOINED ) - + return self end @@ -1154,9 +1154,9 @@ end -- @return #DATABASE self function DATABASE:ForEachPlayerUnit( IteratorFunction, FinalizeFunction, ... ) self:F2( arg ) - + self:ForEach( IteratorFunction, FinalizeFunction, arg, self.PLAYERUNITS ) - + return self end @@ -1167,7 +1167,7 @@ end -- @return #DATABASE self function DATABASE:ForEachClient( IteratorFunction, ... ) self:F2( arg ) - + self:ForEach( IteratorFunction, arg, self.CLIENTS ) return self @@ -1179,7 +1179,7 @@ end -- @return #DATABASE self function DATABASE:ForEachCargo( IteratorFunction, ... ) self:F2( arg ) - + self:ForEach( IteratorFunction, arg, self.CARGOS ) return self @@ -1306,7 +1306,7 @@ function DATABASE:_RegisterTemplates() if (CoalitionName == 'red' or CoalitionName == 'blue' or CoalitionName == 'neutrals') and type(coa_data) == 'table' then --self.Units[coa_name] = {} - + local CoalitionSide = coalition.side[string.upper(CoalitionName)] if CoalitionName=="red" then CoalitionSide=coalition.side.RED @@ -1339,10 +1339,10 @@ function DATABASE:_RegisterTemplates() local CountryName = string.upper(cntry_data.name) local CountryID = cntry_data.id - + self.COUNTRY_ID[CountryName] = CountryID self.COUNTRY_NAME[CountryID] = CountryName - + --self.Units[coa_name][countryName] = {} --self.Units[coa_name][countryName]["countryId"] = cntry_data.id @@ -1361,18 +1361,18 @@ function DATABASE:_RegisterTemplates() for group_num, Template in pairs(obj_type_data.group) do if obj_type_name ~= "static" and Template and Template.units and type(Template.units) == 'table' then --making sure again- this is a valid group - self:_RegisterGroupTemplate( - Template, - CoalitionSide, - _DATABASECategory[string.lower(CategoryName)], - CountryID + self:_RegisterGroupTemplate( + Template, + CoalitionSide, + _DATABASECategory[string.lower(CategoryName)], + CountryID ) else - self:_RegisterStaticTemplate( - Template, - CoalitionSide, - _DATABASECategory[string.lower(CategoryName)], - CountryID + self:_RegisterStaticTemplate( + Template, + CoalitionSide, + _DATABASECategory[string.lower(CategoryName)], + CountryID ) end --if GroupTemplate and GroupTemplate.units then end --for group_num, GroupTemplate in pairs(obj_type_data.group) do @@ -1393,35 +1393,35 @@ end -- @param Core.Event#EVENTDATA Event function DATABASE:AccountHits( Event ) self:F( { Event } ) - + if Event.IniPlayerName ~= nil then -- It is a player that is hitting something self:T( "Hitting Something" ) - + -- What is he hitting? if Event.TgtCategory then - + -- A target got hit self.HITS[Event.TgtUnitName] = self.HITS[Event.TgtUnitName] or {} local Hit = self.HITS[Event.TgtUnitName] - + Hit.Players = Hit.Players or {} Hit.Players[Event.IniPlayerName] = true end end - + -- It is a weapon initiated by a player, that is hitting something -- This seems to occur only with scenery and static objects. - if Event.WeaponPlayerName ~= nil then + if Event.WeaponPlayerName ~= nil then self:T( "Hitting Scenery" ) - + -- What is he hitting? if Event.TgtCategory then - + if Event.WeaponCoalition then -- A coalition object was hit, probably a static. -- A target got hit self.HITS[Event.TgtUnitName] = self.HITS[Event.TgtUnitName] or {} local Hit = self.HITS[Event.TgtUnitName] - + Hit.Players = Hit.Players or {} Hit.Players[Event.WeaponPlayerName] = true else -- A scenery object was hit. @@ -1429,13 +1429,13 @@ end end end end - + --- Account the destroys. -- @param #DATABASE self -- @param Core.Event#EVENTDATA Event function DATABASE:AccountDestroys( Event ) self:F( { Event } ) - + local TargetUnit = nil local TargetGroup = nil local TargetUnitName = "" @@ -1447,26 +1447,26 @@ end local TargetUnitCoalition = nil local TargetUnitCategory = nil local TargetUnitType = nil - + if Event.IniDCSUnit then - + TargetUnit = Event.IniUnit TargetUnitName = Event.IniDCSUnitName TargetGroup = Event.IniDCSGroup TargetGroupName = Event.IniDCSGroupName TargetPlayerName = Event.IniPlayerName - + TargetCoalition = Event.IniCoalition --TargetCategory = TargetUnit:getCategory() --TargetCategory = TargetUnit:getDesc().category -- Workaround TargetCategory = Event.IniCategory TargetType = Event.IniTypeName - + TargetUnitType = TargetType - + self:T( { TargetUnitName, TargetGroupName, TargetPlayerName, TargetCoalition, TargetCategory, TargetType } ) end - + local Destroyed = false -- What is the player destroying? @@ -1475,8 +1475,3 @@ end self.DESTROYS[Event.IniUnitName] = true end end - - - - - diff --git a/Moose Development/Moose/Wrapper/Airbase.lua b/Moose Development/Moose/Wrapper/Airbase.lua index 55c78a30c..9482c97dd 100644 --- a/Moose Development/Moose/Wrapper/Airbase.lua +++ b/Moose Development/Moose/Wrapper/Airbase.lua @@ -1,13 +1,13 @@ --- **Wrapper** -- AIRBASE is a wrapper class to handle the DCS Airbase objects. --- +-- -- === --- +-- -- ### Author: **FlightControl** --- +-- -- ### Contributions: **funkyfranky** --- +-- -- === --- +-- -- @module Wrapper.Airbase -- @image Wrapper_Airbase.JPG @@ -19,40 +19,40 @@ -- @extends Wrapper.Positionable#POSITIONABLE --- Wrapper class to handle the DCS Airbase objects: --- +-- -- * Support all DCS Airbase APIs. -- * Enhance with Airbase specific APIs not in the DCS Airbase API set. --- +-- -- ## AIRBASE reference methods --- +-- -- For each DCS Airbase object alive within a running mission, a AIRBASE wrapper object (instance) will be created within the _@{DATABASE} object. -- This is done at the beginning of the mission (when the mission starts). --- +-- -- The AIRBASE class **does not contain a :New()** method, rather it provides **:Find()** methods to retrieve the object reference -- using the DCS Airbase or the DCS AirbaseName. --- --- Another thing to know is that AIRBASE objects do not "contain" the DCS Airbase object. +-- +-- Another thing to know is that AIRBASE objects do not "contain" the DCS Airbase object. -- The AIRBASE methods will reference the DCS Airbase object by name when it is needed during API execution. -- If the DCS Airbase object does not exist or is nil, the AIRBASE methods will return nil and log an exception in the DCS.log file. --- +-- -- The AIRBASE class provides the following functions to retrieve quickly the relevant AIRBASE instance: --- +-- -- * @{#AIRBASE.Find}(): Find a AIRBASE instance from the _DATABASE object using a DCS Airbase object. -- * @{#AIRBASE.FindByName}(): Find a AIRBASE instance from the _DATABASE object using a DCS Airbase name. --- +-- -- IMPORTANT: ONE SHOULD NEVER SANATIZE these AIRBASE OBJECT REFERENCES! (make the AIRBASE object references nil). --- +-- -- ## DCS Airbase APIs --- +-- -- The DCS Airbase APIs are used extensively within MOOSE. The AIRBASE class has for each DCS Airbase API a corresponding method. -- To be able to distinguish easily in your code the difference between a AIRBASE API call and a DCS Airbase API call, -- the first letter of the method is also capitalized. So, by example, the DCS Airbase method @{DCSWrapper.Airbase#Airbase.getName}() -- is implemented in the AIRBASE class as @{#AIRBASE.GetName}(). --- +-- -- @field #AIRBASE AIRBASE AIRBASE = { ClassName="AIRBASE", - CategoryName = { + CategoryName = { [Airbase.Category.AIRDROME] = "Airdrome", [Airbase.Category.HELIPAD] = "Helipad", [Airbase.Category.SHIP] = "Ship", @@ -61,9 +61,9 @@ AIRBASE = { } --- Enumeration to identify the airbases in the Caucasus region. --- +-- -- These are all airbases of Caucasus: --- +-- -- * AIRBASE.Caucasus.Gelendzhik -- * AIRBASE.Caucasus.Krasnodar_Pashkovsky -- * AIRBASE.Caucasus.Sukhumi_Babushara @@ -85,7 +85,7 @@ AIRBASE = { -- * AIRBASE.Caucasus.Nalchik -- * AIRBASE.Caucasus.Mozdok -- * AIRBASE.Caucasus.Beslan --- +-- -- @field Caucasus AIRBASE.Caucasus = { ["Gelendzhik"] = "Gelendzhik", @@ -112,7 +112,7 @@ AIRBASE.Caucasus = { } --- These are all airbases of Nevada: --- +-- -- * AIRBASE.Nevada.Creech_AFB -- * AIRBASE.Nevada.Groom_Lake_AFB -- * AIRBASE.Nevada.McCarran_International_Airport @@ -130,7 +130,7 @@ AIRBASE.Caucasus = { -- * AIRBASE.Nevada.Pahute_Mesa_Airstrip -- * AIRBASE.Nevada.Tonopah_Airport -- * AIRBASE.Nevada.Tonopah_Test_Range_Airfield --- @field Nevada +-- @field Nevada AIRBASE.Nevada = { ["Creech_AFB"] = "Creech AFB", ["Groom_Lake_AFB"] = "Groom Lake AFB", @@ -152,7 +152,7 @@ AIRBASE.Nevada = { } --- These are all airbases of Normandy: --- +-- -- * AIRBASE.Normandy.Saint_Pierre_du_Mont -- * AIRBASE.Normandy.Lignerolles -- * AIRBASE.Normandy.Cretteville @@ -219,11 +219,11 @@ AIRBASE.Normandy = { ["Ford_AF"] = "Ford_AF", ["Goulet"] = "Goulet", ["Argentan"] = "Argentan", - ["Vrigny"] = "Vrigny", + ["Vrigny"] = "Vrigny", ["Essay"] = "Essay", ["Hauterive"] = "Hauterive", ["Barville"] = "Barville", - ["Conches"] = "Conches", + ["Conches"] = "Conches", } --- These are all airbases of the Persion Gulf Map: @@ -269,7 +269,7 @@ AIRBASE.PersianGulf = { ["Bandar_Abbas_Intl"] = "Bandar Abbas Intl", ["Bandar_Lengeh"] = "Bandar Lengeh", ["Bandar_e_Jask_airfield"] = "Bandar-e-Jask airfield", - ["Dubai_Intl"] = "Dubai Intl", + ["Dubai_Intl"] = "Dubai Intl", ["Fujairah_Intl"] = "Fujairah Intl", ["Havadarya"] = "Havadarya", ["Jiroft_Airport"] = "Jiroft Airport", @@ -301,7 +301,7 @@ AIRBASE.PersianGulf = { -- * AIRBASE.TheChannel.Lympne -- * AIRBASE.TheChannel.Detling -- * AIRBASE.TheChannel.High_Halden --- +-- -- @field TheChannel AIRBASE.TheChannel = { ["Abbeville_Drucat"] = "Abbeville Drucat", @@ -314,7 +314,7 @@ AIRBASE.TheChannel = { ["Detling"] = "Detling", ["High_Halden"] = "High Halden", } - + --- AIRBASE.ParkingSpot ".Coordinate, ".TerminalID", ".TerminalType", ".TOAC", ".Free", ".TerminalID0", ".DistToRwy". -- @type AIRBASE.ParkingSpot -- @field Core.Point#COORDINATE Coordinate Coordinate of the parking spot. @@ -324,11 +324,11 @@ AIRBASE.TheChannel = { -- @field #boolean Free This spot is currently free, i.e. there is no alive aircraft on it at the present moment. -- @field #number TerminalID0 Unknown what this means. If you know, please tell us! -- @field #number DistToRwy Distance to runway in meters. Currently bugged and giving the same number as the TerminalID. - + --- Terminal Types of parking spots. See also https://wiki.hoggitworld.com/view/DCS_func_getParking --- +-- -- Supported types are: --- +-- -- * AIRBASE.TerminalType.Runway = 16: Valid spawn points on runway. -- * AIRBASE.TerminalType.HelicopterOnly = 40: Special spots for Helicopers. -- * AIRBASE.TerminalType.Shelter = 68: Hardened Air Shelter. Currently only on Caucaus map. @@ -337,7 +337,7 @@ AIRBASE.TheChannel = { -- * AIRBASE.TerminalType.OpenMedOrBig = 176: Combines OpenMed and OpenBig spots. -- * AIRBASE.TerminalType.HelicopterUsable = 216: Combines HelicopterOnly, OpenMed and OpenBig. -- * AIRBASE.TerminalType.FighterAircraft = 244: Combines Shelter. OpenMed and OpenBig spots. So effectively all spots usable by fixed wing aircraft. --- +-- -- @type AIRBASE.TerminalType -- @field #number Runway 16: Valid spawn points on runway. -- @field #number HelicopterOnly 40: Special spots for Helicopers. @@ -367,7 +367,7 @@ AIRBASE.TerminalType = { -- @field Core.Point#COORDINATE endpoint End point of runway. -- Registration. - + --- Create a new AIRBASE from DCSAirbase. -- @param #AIRBASE self -- @param #string AirbaseName The name of the airbase. @@ -383,7 +383,7 @@ function AIRBASE:Register( AirbaseName ) else self:E(string.format("ERROR: Cound not get position Vec2 of airbase %s", AirbaseName)) end - + return self end @@ -405,7 +405,7 @@ end -- @param #string AirbaseName The Airbase Name. -- @return #AIRBASE self function AIRBASE:FindByName( AirbaseName ) - + local AirbaseFound = _DATABASE:FindAirbase( AirbaseName ) return AirbaseFound end @@ -415,18 +415,18 @@ end -- @param #number id Airbase ID. -- @return #AIRBASE self function AIRBASE:FindByID(id) - + for name,_airbase in pairs(_DATABASE.AIRBASES) do local airbase=_airbase --#AIRBASE - + local aid=tonumber(airbase:GetID(true)) - + if aid==id then return airbase end - + end - + return nil end @@ -435,11 +435,11 @@ end -- @return DCS#Airbase DCS airbase object. function AIRBASE:GetDCSObject() local DCSAirbase = Airbase.getByName( self.AirbaseName ) - + if DCSAirbase then return DCSAirbase end - + return nil end @@ -455,7 +455,7 @@ end -- @param #number category (Optional) Return only airbases of a certain category, e.g. Airbase.Category.FARP -- @return #table Table containing all airbase objects of the current map. function AIRBASE.GetAllAirbases(coalition, category) - + local airbases={} for _,_airbase in pairs(_DATABASE.AIRBASES) do local airbase=_airbase --#AIRBASE @@ -465,59 +465,59 @@ function AIRBASE.GetAllAirbases(coalition, category) end end end - + return airbases end --- Get ID of the airbase. -- @param #AIRBASE self --- @param #boolean unique (Optional) If true, ships will get a negative sign as the unit ID might be the same as an airbase ID. Default off! +-- @param #boolean unique (Optional) If true, ships will get a negative sign as the unit ID might be the same as an airbase ID. Default off! -- @return #number The airbase ID. function AIRBASE:GetID(unique) if self.AirbaseID then - + return unique and self.AirbaseID or math.abs(self.AirbaseID) - + else - + for DCSAirbaseId, DCSAirbase in ipairs(world.getAirbases()) do - + -- Get the airbase name. local AirbaseName = DCSAirbase:getName() - + -- This gives the incorrect value to be inserted into the airdromeID for DCS 2.5.6! local airbaseID=tonumber(DCSAirbase:getID()) - + local airbaseCategory=self:GetAirbaseCategory() - + --env.info(string.format("FF airbase=%s id=%s category=%s", tostring(AirbaseName), tostring(airbaseID), tostring(airbaseCategory))) - + -- No way AFIK to get the DCS version. So we check if the event exists. That should tell us if we are on DCS 2.5.6 or prior to that. --[[ if world.event.S_EVENT_KILL and world.event.S_EVENT_KILL>0 and airbaseCategory==Airbase.Category.AIRDROME then - + -- We have to take the key value of this loop! airbaseID=DCSAirbaseId - + -- Now another quirk: for Caucasus, we need to add 11 to the key value to get the correct ID. See https://forums.eagle.ru/showpost.php?p=4210774&postcount=11 if UTILS.GetDCSMap()==DCSMAP.Caucasus then - airbaseID=airbaseID+11 + airbaseID=airbaseID+11 end end ]] - + if AirbaseName==self.AirbaseName then if airbaseCategory==Airbase.Category.SHIP then -- Ships get a negative sign as their unit number might be the same as the ID of another airbase. return unique and -airbaseID or airbaseID else return airbaseID - end + end end - + end - + end return nil @@ -525,22 +525,22 @@ end --- Returns a table of parking data for a given airbase. If the optional parameter *available* is true only available parking will be returned, otherwise all parking at the base is returned. Term types have the following enumerated values: --- +-- -- * 16 : Valid spawn points on runway --- * 40 : Helicopter only spawn +-- * 40 : Helicopter only spawn -- * 68 : Hardened Air Shelter -- * 72 : Open/Shelter air airplane only -- * 104: Open air spawn --- +-- -- Note that only Caucuses will return 68 as it is the only map currently with hardened air shelters. -- 104 are also generally larger, but does not guarantee a large aircraft like the B-52 or a C-130 are capable of spawning there. --- +-- -- Table entries: --- +-- -- * Term_index is the id for the parking -- * vTerminal pos is its vec3 position in the world -- * fDistToRW is the distance to the take-off position for the active runway from the parking. --- +-- -- @param #AIRBASE self -- @param #boolean available If true, only available parking spots will be returned. -- @return #table Table with parking data. See https://wiki.hoggitworld.com/view/DCS_func_getParking @@ -549,13 +549,13 @@ function AIRBASE:GetParkingData(available) -- Get DCS airbase object. local DCSAirbase=self:GetDCSObject() - + -- Get parking data. local parkingdata=nil if DCSAirbase then parkingdata=DCSAirbase:getParking(available) end - + self:T2({parkingdata=parkingdata}) return parkingdata end @@ -568,27 +568,27 @@ function AIRBASE:GetParkingSpotsNumber(termtype) -- Get free parking spots data. local parkingdata=self:GetParkingData(false) - + local nspots=0 for _,parkingspot in pairs(parkingdata) do if AIRBASE._CheckTerminalType(parkingspot.Term_Type, termtype) then nspots=nspots+1 end end - + return nspots end --- Get number of free parking spots at an airbase. -- @param #AIRBASE self -- @param #AIRBASE.TerminalType termtype Terminal type. --- @param #boolean allowTOAC If true, spots are considered free even though TO_AC is true. Default is off which is saver to avoid spawning aircraft on top of each other. Option might be enabled for FARPS and ships. +-- @param #boolean allowTOAC If true, spots are considered free even though TO_AC is true. Default is off which is saver to avoid spawning aircraft on top of each other. Option might be enabled for FARPS and ships. -- @return #number Number of free parking spots at this airbase. function AIRBASE:GetFreeParkingSpotsNumber(termtype, allowTOAC) -- Get free parking spots data. local parkingdata=self:GetParkingData(true) - + local nfree=0 for _,parkingspot in pairs(parkingdata) do -- Spots on runway are not counted unless explicitly requested. @@ -598,7 +598,7 @@ function AIRBASE:GetFreeParkingSpotsNumber(termtype, allowTOAC) end end end - + return nfree end @@ -611,7 +611,7 @@ function AIRBASE:GetFreeParkingSpotsCoordinates(termtype, allowTOAC) -- Get free parking spots data. local parkingdata=self:GetParkingData(true) - + -- Put coordinates of free spots into table. local spots={} for _,parkingspot in pairs(parkingdata) do @@ -622,7 +622,7 @@ function AIRBASE:GetFreeParkingSpotsCoordinates(termtype, allowTOAC) end end end - + return spots end @@ -634,23 +634,23 @@ function AIRBASE:GetParkingSpotsCoordinates(termtype) -- Get all parking spots data. local parkingdata=self:GetParkingData(false) - + -- Put coordinates of free spots into table. local spots={} for _,parkingspot in ipairs(parkingdata) do - + -- Coordinates on runway are not returned unless explicitly requested. if AIRBASE._CheckTerminalType(parkingspot.Term_Type, termtype) then - + -- Get coordinate from Vec3 terminal position. local _coord=COORDINATE:NewFromVec3(parkingspot.vTerminalPos) - + -- Add to table. table.insert(spots, _coord) end - + end - + return spots end @@ -661,11 +661,11 @@ end -- @return #table Table free parking spots. Table has the elements ".Coordinate, ".TerminalID", ".TerminalType", ".TOAC", ".Free", ".TerminalID0", ".DistToRwy". function AIRBASE:GetParkingSpotsTable(termtype) - -- Get parking data of all spots (free or occupied) + -- Get parking data of all spots (free or occupied) local parkingdata=self:GetParkingData(false) -- Get parking data of all free spots. local parkingfree=self:GetParkingData(true) - + -- Function to ckeck if any parking spot is free. local function _isfree(_tocheck) for _,_spot in pairs(parkingfree) do @@ -675,7 +675,7 @@ function AIRBASE:GetParkingSpotsTable(termtype) end return false end - + -- Put coordinates of parking spots into table. local spots={} for _,_spot in pairs(parkingdata) do @@ -686,22 +686,22 @@ function AIRBASE:GetParkingSpotsTable(termtype) table.insert(spots, {Coordinate=_coord, TerminalID=_spot.Term_Index, TerminalType=_spot.Term_Type, TOAC=_spot.TO_AC, Free=_free, TerminalID0=_spot.Term_Index_0, DistToRwy=_spot.fDistToRW}) end end - + self:T2({ spots = spots } ) - + return spots end --- Get a table containing the coordinates, terminal index and terminal type of free parking spots at an airbase. -- @param #AIRBASE self -- @param #AIRBASE.TerminalType termtype Terminal type. --- @param #boolean allowTOAC If true, spots are considered free even though TO_AC is true. Default is off which is saver to avoid spawning aircraft on top of each other. Option might be enabled for FARPS and ships. +-- @param #boolean allowTOAC If true, spots are considered free even though TO_AC is true. Default is off which is saver to avoid spawning aircraft on top of each other. Option might be enabled for FARPS and ships. -- @return #table Table free parking spots. Table has the elements ".Coordinate, ".TerminalID", ".TerminalType", ".TOAC", ".Free", ".TerminalID0", ".DistToRwy". function AIRBASE:GetFreeParkingSpotsTable(termtype, allowTOAC) -- Get parking data of all free spots. local parkingfree=self:GetParkingData(true) - + -- Put coordinates of free spots into table. local freespots={} for _,_spot in pairs(parkingfree) do @@ -712,7 +712,7 @@ function AIRBASE:GetFreeParkingSpotsTable(termtype, allowTOAC) end end end - + return freespots end @@ -725,10 +725,10 @@ function AIRBASE:GetParkingSpotData(TerminalID) -- Get parking data. local parkingdata=self:GetParkingSpotsTable() - + -- Debug output. self:T2({parkingdata=parkingdata}) - + for _,_spot in pairs(parkingdata) do local spot=_spot --#AIRBASE.ParkingSpot self:T({TerminalID=spot.TerminalID,TerminalType=spot.TerminalType}) @@ -736,7 +736,7 @@ function AIRBASE:GetParkingSpotData(TerminalID) return spot end end - + self:E("ERROR: Could not find spot with Terminal ID="..tostring(TerminalID)) return nil end @@ -758,18 +758,18 @@ function AIRBASE:MarkParkingSpots(termtype, mark) -- Get airbase name. local airbasename=self:GetName() self:E(string.format("Parking spots at %s for termial type %s:", airbasename, tostring(termtype))) - + for _,_spot in pairs(parkingdata) do - + -- Mark text. local _text=string.format("Term Index=%d, Term Type=%d, Free=%s, TOAC=%s, Term ID0=%d, Dist2Rwy=%.1f m", _spot.TerminalID, _spot.TerminalType,tostring(_spot.Free),tostring(_spot.TOAC),_spot.TerminalID0,_spot.DistToRwy) - + -- Create mark on the F10 map. if mark then _spot.Coordinate:MarkToAll(_text) end - + -- Info to DCS.log file. local _text=string.format("%s, Term Index=%3d, Term Type=%03d, Free=%5s, TOAC=%5s, Term ID0=%3d, Dist2Rwy=%.1f m", airbasename, _spot.TerminalID, _spot.TerminalType,tostring(_spot.Free),tostring(_spot.TOAC),_spot.TerminalID0,_spot.DistToRwy) @@ -787,7 +787,7 @@ end -- @param #boolean scanstatics (Optional) Scan for statics as obstacles. Default true. -- @param #boolean scanscenery (Optional) Scan for scenery as obstacles. Default false. Can cause problems with e.g. shelters. -- @param #boolean verysafe (Optional) If true, wait until an aircraft has taken off until the parking spot is considered to be free. Defaul false. --- @param #number nspots (Optional) Number of freeparking spots requested. Default is the number of aircraft in the group. +-- @param #number nspots (Optional) Number of freeparking spots requested. Default is the number of aircraft in the group. -- @param #table parkingdata (Optional) Parking spots data table. If not given it is automatically derived from the GetParkingSpotsTable() function. -- @return #table Table of coordinates and terminal IDs of free parking spots. Each table entry has the elements .Coordinate and .TerminalID. function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, scanunits, scanstatics, scanscenery, verysafe, nspots, parkingdata) @@ -805,8 +805,8 @@ function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, end if verysafe==nil then verysafe=false - end - + end + -- Function calculating the overlap of two (square) objects. local function _overlap(object1, object2, dist) local pos1=object1 --Wrapper.Positionable#POSITIONABLE @@ -814,107 +814,107 @@ function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, local r1=pos1:GetBoundingRadius() local r2=pos2:GetBoundingRadius() if r1 and r2 then - local safedist=(r1+r2)*1.1 + local safedist=(r1+r2)*1.1 local safe = (dist > safedist) self:T2(string.format("r1=%.1f r2=%.1f s=%.1f d=%.1f ==> safe=%s", r1, r2, safedist, dist, tostring(safe))) return safe else return true - end + end end - + -- Get airport name. local airport=self:GetName() - + -- Get parking spot data table. This contains free and "non-free" spots. -- Note that there are three major issues with the DCS getParking() function: -- 1. A spot is considered as NOT free until an aircraft that is present has finally taken off. This might be a bit long especiall at smaller airports. -- 2. A "free" spot does not take the aircraft size into accound. So if two big aircraft are spawned on spots next to each other, they might overlap and get destroyed. -- 3. The routine return a free spot, if there a static objects placed on the spot. parkingdata=parkingdata or self:GetParkingSpotsTable(terminaltype) - + -- Get the aircraft size, i.e. it's longest side of x,z. local aircraft=group:GetUnit(1) local _aircraftsize, ax,ay,az=aircraft:GetObjectSize() - + -- Number of spots we are looking for. Note that, e.g. grouping can require a number different from the group size! local _nspots=nspots or group:GetSize() - + -- Debug info. self:E(string.format("%s: Looking for %d parking spot(s) for aircraft of size %.1f m (x=%.1f,y=%.1f,z=%.1f) at termial type %s.", airport, _nspots, _aircraftsize, ax, ay, az, tostring(terminaltype))) - + -- Table of valid spots. local validspots={} local nvalid=0 - + -- Test other stuff if no parking spot is available. local _test=false if _test then return validspots end - + -- Mark all found obstacles on F10 map for debugging. local markobstacles=false - + -- Loop over all known parking spots for _,parkingspot in pairs(parkingdata) do - + -- Coordinate of the parking spot. local _spot=parkingspot.Coordinate -- Core.Point#COORDINATE local _termid=parkingspot.TerminalID - + self:T2({_termid=_termid}) - + if AIRBASE._CheckTerminalType(parkingspot.TerminalType, terminaltype) then - + -- Very safe uses the DCS getParking() info to check if a spot is free. Unfortunately, the function returns free=false until the aircraft has actually taken-off. if verysafe and (parkingspot.Free==false or parkingspot.TOAC==true) then - + -- DCS getParking() routine returned that spot is not free. self:T(string.format("%s: Parking spot id %d NOT free (or aircraft has not taken off yet). Free=%s, TOAC=%s.", airport, parkingspot.TerminalID, tostring(parkingspot.Free), tostring(parkingspot.TOAC))) - + else - + -- Scan a radius of 50 meters around the spot. local _,_,_,_units,_statics,_sceneries=_spot:ScanObjects(scanradius, scanunits, scanstatics, scanscenery) - + -- Loop over objects within scan radius. local occupied=false - - -- Check all units. + + -- Check all units. for _,unit in pairs(_units) do local _coord=unit:GetCoordinate() - local _dist=_coord:Get2DDistance(_spot) + local _dist=_coord:Get2DDistance(_spot) local _safe=_overlap(aircraft, unit, _dist) - + if markobstacles then local l,x,y,z=unit:GetObjectSize() _coord:MarkToAll(string.format("Unit %s\nx=%.1f y=%.1f z=%.1f\nl=%.1f d=%.1f\nspot %d safe=%s", unit:GetName(),x,y,z,l,_dist, _termid, tostring(_safe))) end - + if scanunits and not _safe then occupied=true - end + end end - + -- Check all statics. for _,static in pairs(_statics) do local _static=STATIC:Find(static) local _vec3=static:getPoint() local _coord=COORDINATE:NewFromVec3(_vec3) - local _dist=_coord:Get2DDistance(_spot) + local _dist=_coord:Get2DDistance(_spot) local _safe=_overlap(aircraft,_static,_dist) - + if markobstacles then local l,x,y,z=_static:GetObjectSize() _coord:MarkToAll(string.format("Static %s\nx=%.1f y=%.1f z=%.1f\nl=%.1f d=%.1f\nspot %d safe=%s", static:getName(),x,y,z,l,_dist, _termid, tostring(_safe))) end - + if scanstatics and not _safe then occupied=true - end + end end - + -- Check all scenery. for _,scenery in pairs(_sceneries) do local _scenery=SCENERY:Register(scenery:getTypeName(), scenery) @@ -922,17 +922,17 @@ function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, local _coord=COORDINATE:NewFromVec3(_vec3) local _dist=_coord:Get2DDistance(_spot) local _safe=_overlap(aircraft,_scenery,_dist) - + if markobstacles then local l,x,y,z=scenery:GetObjectSize(scenery) _coord:MarkToAll(string.format("Scenery %s\nx=%.1f y=%.1f z=%.1f\nl=%.1f d=%.1f\nspot %d safe=%s", scenery:getTypeName(),x,y,z,l,_dist, _termid, tostring(_safe))) end - + if scanscenery and not _safe then occupied=true - end + end end - + -- Now check the already given spots so that we do not put a large aircraft next to one we already assigned a nearby spot. for _,_takenspot in pairs(validspots) do local _dist=_takenspot.Coordinate:Get2DDistance(_spot) @@ -941,7 +941,7 @@ function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, occupied=true end end - + --_spot:MarkToAll(string.format("Parking spot %d free=%s", parkingspot.TerminalID, tostring(not occupied))) if occupied then self:I(string.format("%s: Parking spot id %d occupied.", airport, _termid)) @@ -953,16 +953,16 @@ function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, nvalid=nvalid+1 self:I(string.format("%s: Parking spot id %d free. Nfree=%d/%d.", airport, _termid, nvalid,_nspots)) end - + end -- loop over units - + -- We found enough spots. if nvalid>=_nspots then return validspots end end -- check terminal type - end - + end + -- Retrun spots we found, even if there were not enough. return validspots end @@ -971,54 +971,54 @@ end -- @param #AIRBASE self -- @param Wrapper.Group#GROUP group Group to be checked. -- @param #number radius Radius around the spawn point to be checked. Default is 50 m. --- @param #boolean despawn If true, the group is destroyed. +-- @param #boolean despawn If true, the group is destroyed. -- @return #boolean True if group is within radius around spawn points on runway. function AIRBASE:CheckOnRunWay(group, radius, despawn) -- Default radius. radius=radius or 50 - + -- We only check at real airbases (not FARPS or ships). if self:GetAirbaseCategory()~=Airbase.Category.AIRDROME then return false end if group and group:IsAlive() then - + -- Debug. self:T(string.format("%s, checking if group %s is on runway?",self:GetName(), group:GetName())) - + -- Get coordinates on runway. local runwaypoints=self:GetParkingSpotsCoordinates(AIRBASE.TerminalType.Runway) - + -- Mark runway spawn points. --[[ for _i,_coord in pairs(runwaypoints) do _coord:MarkToAll(string.format("runway %d",_i)) end ]] - + -- Get units of group. local units=group:GetUnits() - + -- Loop over units. for _,_unit in pairs(units) do - + local unit=_unit --Wrapper.Unit#UNIT - + -- Check if unit is alive and not in air. if unit and unit:IsAlive() and not unit:InAir() then self:T(string.format("%s, checking if unit %s is on runway?",self:GetName(), unit:GetName())) - + -- Loop over runway spawn points. for _i,_coord in pairs(runwaypoints) do -- Distance between unit and spawn pos. local dist=unit:GetCoordinate():Get2DDistance(_coord) - + -- Mark unit spawn points for debugging. --unit:GetCoordinate():MarkToAll(string.format("unit %s distance to rwy %d = %d",unit:GetName(),_i, dist)) - + -- Check if unit is withing radius. if dist radius %.1f m. Despawn = %s.", self:GetName(), unit:GetName(), group:GetName(),_i, dist, radius, tostring(despawn))) --unit:FlareGreen() end - + end else - self:T(string.format("%s, checking if unit %s of group %s is on runway. Unit is NOT alive.",self:GetName(), unit:GetName(), group:GetName())) - end + self:T(string.format("%s, checking if unit %s of group %s is on runway. Unit is NOT alive.",self:GetName(), unit:GetName(), group:GetName())) + end end else self:T(string.format("%s, checking if group %s is on runway. Group is NOT alive.",self:GetName(), group:GetName())) end - + return false end @@ -1079,15 +1079,15 @@ function AIRBASE._CheckTerminalType(Term_Type, termtype) return true end end - + -- Init no match. local match=false - - -- Standar case. + + -- Standar case. if Term_Type==termtype then match=true end - + -- Artificial cases. Combination of terminal types. if termtype==AIRBASE.TerminalType.OpenMedOrBig then if Term_Type==AIRBASE.TerminalType.OpenMed or Term_Type==AIRBASE.TerminalType.OpenBig then @@ -1102,7 +1102,7 @@ function AIRBASE._CheckTerminalType(Term_Type, termtype) match=true end end - + return match end @@ -1115,106 +1115,106 @@ function AIRBASE:GetRunwayData(magvar, mark) -- Runway table. local runways={} - + if self:GetAirbaseCategory()~=Airbase.Category.AIRDROME then return {} end -- Get spawn points on runway. local runwaycoords=self:GetParkingSpotsCoordinates(AIRBASE.TerminalType.Runway) - + -- Magnetic declination. magvar=magvar or UTILS.GetMagneticDeclination() - + local N=#runwaycoords local dN=2 local ex=false - + local name=self:GetName() - if name==AIRBASE.Nevada.Jean_Airport or - name==AIRBASE.Nevada.Creech_AFB or + if name==AIRBASE.Nevada.Jean_Airport or + name==AIRBASE.Nevada.Creech_AFB or name==AIRBASE.PersianGulf.Abu_Dhabi_International_Airport or name==AIRBASE.PersianGulf.Dubai_Intl or name==AIRBASE.PersianGulf.Shiraz_International_Airport or name==AIRBASE.PersianGulf.Kish_International_Airport then - + N=#runwaycoords/2 dN=1 ex=true end - - + + for i=1,N,dN do - + local j=i+1 if ex then --j=N+i j=#runwaycoords-i+1 end - + -- Coordinates of the two runway points. local c1=runwaycoords[i] --Core.Point#COORDINATES local c2=runwaycoords[j] --Core.Point#COORDINATES - + -- Heading of runway. local hdg=c1:HeadingTo(c2) - + -- Runway ID: heading=070° ==> idx="07" local idx=string.format("%02d", UTILS.Round((hdg-magvar)/10, 0)) - + -- Runway table. local runway={} --#AIRBASE.Runway runway.heading=hdg runway.idx=idx - runway.length=c1:Get2DDistance(c2) + runway.length=c1:Get2DDistance(c2) runway.position=c1 runway.endpoint=c2 - + -- Debug info. self:T(string.format("Airbase %s: Adding runway id=%s, heading=%03d, length=%d m", self:GetName(), runway.idx, runway.heading, runway.length)) - + -- Debug mark if mark then runway.position:MarkToAll(string.format("Runway %s: true heading=%03d (magvar=%d), length=%d m", runway.idx, runway.heading, magvar, runway.length)) end - + -- Add runway. table.insert(runways, runway) - + end - + -- Get inverse runways local inverse={} for _,_runway in pairs(runways) do local r=_runway --#AIRBASE.Runway - - local runway={} --#AIRBASE.Runway + + local runway={} --#AIRBASE.Runway runway.heading=r.heading-180 if runway.heading<0 then runway.heading=runway.heading+360 - end + end runway.idx=string.format("%02d", math.max(0, UTILS.Round((runway.heading-magvar)/10, 0))) runway.length=r.length runway.position=r.endpoint runway.endpoint=r.position - + -- Debug info. self:T(string.format("Airbase %s: Adding runway id=%s, heading=%03d, length=%d m", self:GetName(), runway.idx, runway.heading, runway.length)) - + -- Debug mark if mark then runway.position:MarkToAll(string.format("Runway %s: true heading=%03d (magvar=%d), length=%d m", runway.idx, runway.heading, magvar, runway.length)) end - + -- Add runway. - table.insert(inverse, runway) + table.insert(inverse, runway) end - + -- Add inverse runway. for _,runway in pairs(inverse) do - table.insert(runways, runway) + table.insert(runways, runway) end - + return runways end @@ -1242,48 +1242,45 @@ function AIRBASE:GetActiveRunway(magvar) -- Get wind vector. local Vwind=self:GetCoordinate():GetWindWithTurbulenceVec3() local norm=UTILS.VecNorm(Vwind) - + -- Active runway number. local iact=1 - + -- Check if wind is blowing (norm>0). if norm>0 then - + -- Normalize wind (not necessary). Vwind.x=Vwind.x/norm Vwind.y=0 Vwind.z=Vwind.z/norm - + -- Loop over runways. local dotmin=nil for i,_runway in pairs(runways) do local runway=_runway --#AIRBASE.Runway - + -- Angle in rad. local alpha=math.rad(runway.heading) - + -- Runway vector. local Vrunway={x=math.cos(alpha), y=0, z=math.sin(alpha)} - + -- Dot product: parallel component of the two vectors. local dot=UTILS.VecDot(Vwind, Vrunway) - + -- Debug. --env.info(string.format("runway=%03d° dot=%.3f", runway.heading, dot)) - + -- New min? if dotmin==nil or dot