diff --git a/Moose Development/Moose/Core/Spawn.lua b/Moose Development/Moose/Core/Spawn.lua index 67d1cf753..a9817109e 100644 --- a/Moose Development/Moose/Core/Spawn.lua +++ b/Moose Development/Moose/Core/Spawn.lua @@ -295,7 +295,9 @@ function SPAWN:New( SpawnTemplatePrefix ) self.SpawnUnControlled = false self.SpawnInitKeepUnitNames = false -- Overwrite unit names by default with group name. self.DelayOnOff = false -- No intial delay when spawning the first group. - self.Grouping = nil -- No grouping + self.Grouping = nil -- No grouping. + self.SpawnInitLivery = nil -- No special livery. + self.SpawnInitSkill = nil -- No special skill. self.SpawnGroups = {} -- Array containing the descriptions of each Group to be Spawned. else @@ -341,7 +343,9 @@ function SPAWN:NewWithAlias( SpawnTemplatePrefix, SpawnAliasPrefix ) self.SpawnUnControlled = false self.SpawnInitKeepUnitNames = false -- Overwrite unit names by default with group name. self.DelayOnOff = false -- No intial delay when spawning the first group. - self.Grouping = nil + self.Grouping = nil -- No grouping. + self.SpawnInitLivery = nil -- No special livery. + self.SpawnInitSkill = nil -- No special skill. self.SpawnGroups = {} -- Array containing the descriptions of each Group to be Spawned. else @@ -390,7 +394,9 @@ function SPAWN:NewFromTemplate( SpawnTemplate, SpawnTemplatePrefix, SpawnAliasPr self.SpawnUnControlled = false self.SpawnInitKeepUnitNames = false -- Overwrite unit names by default with group name. self.DelayOnOff = false -- No intial delay when spawning the first group. - self.Grouping = nil + self.Grouping = nil -- No grouping. + self.SpawnInitLivery = nil -- No special livery. + self.SpawnInitSkill = nil -- No special skill. self.SpawnGroups = {} -- Array containing the descriptions of each Group to be Spawned. else @@ -488,15 +494,23 @@ function SPAWN:InitHeading( HeadingMin, HeadingMax ) end +--- Sets the coalition of the spawned group. Note that it might be necessary to also set the country explicitly! +-- @param #SPAWN self +-- @param #DCS.coalition Coaliton Coaliton of the group as number of enumerator, i.e. 0=coaliton.side.NEUTRAL, 1=coaliton.side.RED, 2=coalition.side.BLUE. +-- @return #SPAWN self function SPAWN:InitCoalition( Coalition ) - self:F( ) + self:F({coalition=Coalition}) self.SpawnInitCoalition = Coalition return self end - +--- Sets the country of the spawn group. Note that the country determins the coalition of the group depending on which country is defined to be on which side for each specific mission! +-- See https://wiki.hoggitworld.com/view/DCS_enum_country for country enumerators. +-- @param #SPAWN self +-- @param #DCS.country Country Country id as number or enumerator, e.g. country.id.RUSSIA=0, county.id.USA=2 etc. +-- @return #SPAWN self function SPAWN:InitCountry( Country ) self:F( ) @@ -506,6 +520,10 @@ function SPAWN:InitCountry( Country ) end +--- Sets category ID of the group. +-- @param #SPAWN self +-- @param #number Category Category id. +-- @return #SPAWN self function SPAWN:InitCategory( Category ) self:F( ) @@ -514,6 +532,40 @@ function SPAWN:InitCategory( Category ) return self end +--- Sets livery of the group. +-- @param #SPAWN self +-- @param #string Livery Livery name. Note that this is not necessarily the same name as displayed in the mission edior. +-- @return #SPAWN self +function SPAWN:InitLivery( Livery ) + self:F({livery=Livery} ) + + self.SpawnInitLivery = Livery + + return self +end + +--- Sets skill of the group. +-- @param #SPAWN self +-- @param #string Skill Skill, possible values "Average", "Good", "High", "Excellent" or "Random". +-- @return #SPAWN self +function SPAWN:InitSkill( Skill ) + self:F({skill=Skill}) + if Skill:lower()=="average" then + self.SpawnInitSkill="Average" + elseif Skill:lower()=="good" then + self.SpawnInitSkill="Good" + elseif Skill:lower()=="excellent" then + self.SpawnInitSkill="Excellent" + elseif Skill:lower()=="random" then + self.SpawnInitSkill="Random" + else + self.SpawnInitSkill="High" + end + + return self +end + + --- Randomizes the defined route of the SpawnTemplatePrefix group in the ME. This is very useful to define extra variation of the behaviour of groups. -- @param #SPAWN self -- @param #number SpawnStartPoint is the waypoint where the randomization begins. @@ -1050,13 +1102,40 @@ function SPAWN:SpawnWithIndex( SpawnIndex ) end end + -- Get correct heading. + local function _Heading(course) + local h + if course<=180 then + h=math.rad(course) + else + h=-math.rad(360-course) + end + return h + end + -- If Heading is given, point all the units towards the given Heading. if self.SpawnInitHeadingMin then for UnitID = 1, #SpawnTemplate.units do - SpawnTemplate.units[UnitID].heading = self.SpawnInitHeadingMax and math.random( self.SpawnInitHeadingMin, self.SpawnInitHeadingMax ) or self.SpawnInitHeadingMin + SpawnTemplate.units[UnitID].heading = _Heading(self.SpawnInitHeadingMax and math.random( self.SpawnInitHeadingMin, self.SpawnInitHeadingMax ) or self.SpawnInitHeadingMin) + SpawnTemplate.units[UnitID].psi = -SpawnTemplate.units[UnitID].heading end end + -- Set livery. + if self.SpawnInitLivery then + for UnitID = 1, #SpawnTemplate.units do + SpawnTemplate.units[UnitID].livery_id = self.SpawnInitLivery + end + end + + -- Set skill. + if self.SpawnInitSkill then + for UnitID = 1, #SpawnTemplate.units do + SpawnTemplate.units[UnitID].skill = self.SpawnInitSkill + end + end + + -- Set country, coaliton and categroy. SpawnTemplate.CategoryID = self.SpawnInitCategory or SpawnTemplate.CategoryID SpawnTemplate.CountryID = self.SpawnInitCountry or SpawnTemplate.CountryID SpawnTemplate.CoalitionID = self.SpawnInitCoalition or SpawnTemplate.CoalitionID @@ -1272,9 +1351,12 @@ function SPAWN:SpawnAtAirbase( SpawnAirbase, Takeoff, TakeoffAltitude, TerminalT local isbomber=TemplateUnit:HasAttribute("Bombers") local istransport=TemplateUnit:HasAttribute("Transports") local isfighter=TemplateUnit:HasAttribute("Battleplanes") + + -- Number of units in the group. With grouping this can actually differ from the template group size! + local nunits=#SpawnTemplate.units -- First waypoint of the group. - local SpawnPoint = SpawnTemplate.route.points[1] + local SpawnPoint = SpawnTemplate.route.points[1] -- These are only for ships and FARPS. SpawnPoint.linkUnit = nil @@ -1350,25 +1432,25 @@ function SPAWN:SpawnAtAirbase( SpawnAirbase, Takeoff, TakeoffAltitude, TerminalT if spawnonship or spawnonfarp or spawnonrunway then -- These places work procedural and have some kind of build in queue ==> Less effort. self:T(string.format("Group %s is spawned on farp/ship/runway %s.", self.SpawnTemplatePrefix, SpawnAirbase:GetName())) - nfree=SpawnAirbase:GetFreeParkingSpotsNumber(termtype, spawnonship or spawnonfarp or spawnonrunway) - spots=SpawnAirbase:GetFreeParkingSpotsTable(termtype, spawnonship or spawnonfarp or spawnonrunway) + nfree=SpawnAirbase:GetFreeParkingSpotsNumber(termtype, true) + spots=SpawnAirbase:GetFreeParkingSpotsTable(termtype, true) else if ishelo then if termtype==nil then -- Helo is spawned. Try exclusive helo spots first. self:T(string.format("Helo group %s is at %s using terminal type %d.", self.SpawnTemplatePrefix, SpawnAirbase:GetName(), AIRBASE.TerminalType.HelicopterOnly)) - spots=SpawnAirbase:FindFreeParkingSpotForAircraft(TemplateGroup, AIRBASE.TerminalType.HelicopterOnly, scanradius, scanunits, scanstatics, scanscenery, verysafe) + spots=SpawnAirbase:FindFreeParkingSpotForAircraft(TemplateGroup, AIRBASE.TerminalType.HelicopterOnly, scanradius, scanunits, scanstatics, scanscenery, verysafe, nunits) nfree=#spots - if nfree<#SpawnTemplate.units then + if nfree= #SpawnTemplate.units or (spawnonrunway and nfree>0) then - - for i=1,#SpawnTemplate.units do - table.insert(parkingspots, spots[i].Coordinate) - table.insert(parkingindex, spots[i].TerminalID) + -- Set this to true if not enough spots are available for emergency air start. + local _notenough=false + + -- Need to differentiate some cases again. + if spawnonship or spawnonfarp or spawnonrunway then + + -- On free spot required in these cases. + if nfree >=1 then + + -- All units get the same spot. DCS takes care of the rest. + for i=1,nunits do + table.insert(parkingspots, spots[1].Coordinate) + table.insert(parkingindex, spots[1].TerminalID) + end + -- This is actually used... + PointVec3=spots[1].Coordinate + + else + -- If there is absolutely not spot ==> air start! + _notenough=true end + + elseif spawnonairport then + + if nfree>=nunits then - else + for i=1,nunits do + table.insert(parkingspots, spots[i].Coordinate) + table.insert(parkingindex, spots[i].TerminalID) + end + + else + -- Not enough spots for the whole group ==> air start! + _notenough=true + end + end + + -- Not enough spots ==> Prepare airstart. + if _notenough then + if EmergencyAirSpawn and not self.SpawnUnControlled then self:E(string.format("WARNING: Group %s has no parking spots at %s ==> air start!", self.SpawnTemplatePrefix, SpawnAirbase:GetName())) @@ -1449,11 +1562,25 @@ function SPAWN:SpawnAtAirbase( SpawnAirbase, Takeoff, TakeoffAltitude, TerminalT return nil end end - + + else + + -- Air start requested initially ==> Set altitude. + if TakeoffAltitude then + PointVec3.y=TakeoffAltitude + else + if ishelo then + PointVec3.y=PointVec3:GetLandHeight()+math.random(100,1000) + else + -- Randomize position so that multiple AC wont be spawned on top even in air. + PointVec3.y=PointVec3:GetLandHeight()+math.random(500,2500) + end + end + end -- Translate the position of the Group Template to the Vec3. - for UnitID = 1, #SpawnTemplate.units do + for UnitID = 1, nunits do self:T2('Before Translation SpawnTemplate.units['..UnitID..'].x = '..SpawnTemplate.units[UnitID].x..', SpawnTemplate.units['..UnitID..'].y = '..SpawnTemplate.units[UnitID].y) -- Template of the current unit. diff --git a/Moose Development/Moose/Functional/RAT.lua b/Moose Development/Moose/Functional/RAT.lua index 081106680..142dd60cb 100644 --- a/Moose Development/Moose/Functional/RAT.lua +++ b/Moose Development/Moose/Functional/RAT.lua @@ -511,7 +511,7 @@ RAT.id="RAT | " --- RAT version. -- @list version RAT.version={ - version = "2.3.0", + version = "2.3.1", print = true, } @@ -5031,6 +5031,9 @@ function RAT:_ModifySpawnTemplate(waypoints, livery, spawnplace, departure, take self.SpawnUnControlled=true end + -- Number of units in the group. With grouping this can actually differ from the template group size! + local nunits=#SpawnTemplate.units + -- Array with parking spots coordinates. local parkingspots={} local parkingindex={} @@ -5059,26 +5062,26 @@ function RAT:_ModifySpawnTemplate(waypoints, livery, spawnplace, departure, take if spawnonship or spawnonfarp or spawnonrunway then -- These places work procedural and have some kind of build in queue ==> Less effort. self:T(RAT.id..string.format("Group %s is spawned on farp/ship/runway %s.", self.alias, departure:GetName())) - nfree=departure:GetFreeParkingSpotsNumber(termtype, spawnonship or spawnonfarp or spawnonrunway) - spots=departure:GetFreeParkingSpotsTable(termtype, spawnonship or spawnonfarp or spawnonrunway) + nfree=departure:GetFreeParkingSpotsNumber(termtype, true) + spots=departure:GetFreeParkingSpotsTable(termtype, true) else -- Helo is spawned. if self.category==RAT.cat.heli then if termtype==nil then -- Try exclusive helo spots first. self:T(RAT.id..string.format("Helo group %s is spawned at %s using terminal type %d.", self.alias, departure:GetName(), AIRBASE.TerminalType.HelicopterOnly)) - spots=departure:FindFreeParkingSpotForAircraft(TemplateGroup, AIRBASE.TerminalType.HelicopterOnly, scanradius, scanunits, scanstatics, scanscenery, verysafe) + spots=departure:FindFreeParkingSpotForAircraft(TemplateGroup, AIRBASE.TerminalType.HelicopterOnly, scanradius, scanunits, scanstatics, scanscenery, verysafe, nunits) nfree=#spots - if nfree<#SpawnTemplate.units then + if nfree= #SpawnTemplate.units or (spawnonrunway and nfree>0) then - - for i=1,#SpawnTemplate.units do - table.insert(parkingspots, spots[i].Coordinate) - table.insert(parkingindex, spots[i].TerminalID) + + -- Set this to true if not enough spots are available for emergency air start. + local _notenough=false + + -- Need to differentiate some cases again. + if spawnonship or spawnonfarp or spawnonrunway then + + -- On free spot required in these cases. + if nfree >=1 then + + -- All units get the same spot. DCS takes care of the rest. + for i=1,nunits do + table.insert(parkingspots, spots[1].Coordinate) + table.insert(parkingindex, spots[1].TerminalID) + end + -- This is actually used... + PointVec3=spots[1].Coordinate + + else + -- If there is absolutely not spot ==> air start! + _notenough=true end + + elseif spawnonairport then + + if nfree>=nunits then - else - if self.respawn_inair and not self.uncontrolled then - self:E(RAT.id..string.format("WARNING: RAT group %s has no parking spots at %s ==> air start!", self.alias, departure:GetName())) + for i=1,nunits do + table.insert(parkingspots, spots[i].Coordinate) + table.insert(parkingindex, spots[i].TerminalID) + end + + else + -- Not enough spots for the whole group ==> air start! + _notenough=true + end + end + + -- Not enough spots ==> Prepare airstart. + if _notenough then + + if self.respawn_inair and not self.SpawnUnControlled then + self:E(RAT.id..string.format("WARNING: Group %s has no parking spots at %s ==> air start!", self.SpawnTemplatePrefix, departure:GetName())) -- Not enough parking spots at the airport ==> Spawn in air. spawnonground=false @@ -5142,27 +5177,37 @@ function RAT:_ModifySpawnTemplate(waypoints, livery, spawnplace, departure, take spawnonrunway=false -- Set waypoint type/action to turning point. - waypoints[1].type = GROUPTEMPLATE.Takeoff[GROUP.Takeoff.Air][1] -- type = Turning Point + waypoints[1].type = GROUPTEMPLATE.Takeoff[GROUP.Takeoff.Air][1] -- type = Turning Point waypoints[1].action = GROUPTEMPLATE.Takeoff[GROUP.Takeoff.Air][2] -- action = Turning Point - -- Adjust and randomize position and altitude of the spawn point. + -- Adjust altitude to be 500-1000 m above the airbase. PointVec3.x=PointVec3.x+math.random(-500,500) - PointVec3.z=PointVec3.z+math.random(-500,500) + PointVec3.z=PointVec3.z+math.random(-500,500) if self.category==RAT.cat.heli then PointVec3.y=PointVec3:GetLandHeight()+math.random(100,1000) else + -- Randomize position so that multiple AC wont be spawned on top even in air. PointVec3.y=PointVec3:GetLandHeight()+math.random(500,2500) end else - self:E(RAT.id..string.format("WARNING: RAT group %s has no parking spots at %s. Air start deactivated or uncontrolled AC!", self.alias, departure:GetName())) + self:E(RAT.id..string.format("WARNING: Group %s has no parking spots at %s ==> No emergency air start or uncontrolled spawning ==> No spawn!", self.SpawnTemplatePrefix, departure:GetName())) return nil - end + end end + + else + + -- Air start requested initially! + --PointVec3.y is already set from first waypoint here! + end - + + +--- new + -- Translate the position of the Group Template to the Vec3. - for UnitID = 1, #SpawnTemplate.units do + for UnitID = 1, nunits do -- Template of the current unit. local UnitTemplate = SpawnTemplate.units[UnitID] diff --git a/Moose Development/Moose/Wrapper/Airbase.lua b/Moose Development/Moose/Wrapper/Airbase.lua index 0b4663bb5..635cbfe4a 100644 --- a/Moose Development/Moose/Wrapper/Airbase.lua +++ b/Moose Development/Moose/Wrapper/Airbase.lua @@ -589,8 +589,9 @@ 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. -- @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) +function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, scanunits, scanstatics, scanscenery, verysafe, nspots) -- Init default scanradius=scanradius or 50 @@ -646,11 +647,13 @@ function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, -- Get the aircraft size, i.e. it's longest side of x,z. local aircraft=group:GetUnit(1) - local nspots=group:GetSize() local _aircraftsize, ax,ay,az=_GetObjectSize(aircraft, true) + -- 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("Looking for %d parking spot(s) at %s for aircraft of size %.1f m (x=%.1f,y=%.1f,z=%.1f) at termial type %s.", nspots, airport, _aircraftsize, ax, ay, az, tostring(terminaltype))) + 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={} @@ -743,7 +746,7 @@ function AIRBASE:FindFreeParkingSpotForAircraft(group, terminaltype, scanradius, self:T(string.format("%s: Parking spot id %d occupied.", airport, _termid)) else self:E(string.format("%s: Parking spot id %d free.", airport, _termid)) - if nvalid=nspots then + if nvalid>=_nspots then return validspots end end