From 8440cb01ab206e82e6d44d8d4771bba97aa661f7 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Sat, 8 Apr 2017 14:05:28 +0200 Subject: [PATCH 1/2] First Version --- Moose Development/Moose/Core/SpawnStatic.lua | 172 +++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Moose Development/Moose/Core/SpawnStatic.lua diff --git a/Moose Development/Moose/Core/SpawnStatic.lua b/Moose Development/Moose/Core/SpawnStatic.lua new file mode 100644 index 000000000..a891cf2b3 --- /dev/null +++ b/Moose Development/Moose/Core/SpawnStatic.lua @@ -0,0 +1,172 @@ +--- **Functional** -- Spawn dynamically new @{Static}s in your missions. +-- +-- ![Banner Image](..\Presentations\SPAWNSTATIC\SPAWNSTATIC.JPG) +-- +-- ==== +-- +-- # Demo Missions +-- +-- ### [SPAWNSTATIC Demo Missions source code]() +-- +-- ### [SPAWNSTATIC Demo Missions, only for beta testers]() +-- +-- ### [ALL Demo Missions pack of the last release](https://github.com/FlightControl-Master/MOOSE_MISSIONS/releases) +-- +-- ==== +-- +-- # YouTube Channel +-- +-- ### [SPAWNSTATIC YouTube Channel]() +-- +-- ==== +-- +-- # **API CHANGE HISTORY** +-- +-- The underlying change log documents the API changes. Please read this carefully. The following notation is used: +-- +-- * **Added** parts are expressed in bold type face. +-- * _Removed_ parts are expressed in italic type face. +-- +-- Hereby the change log: +-- +-- === +-- +-- # **AUTHORS and CONTRIBUTIONS** +-- +-- ### Contributions: +-- +-- ### Authors: +-- +-- * **FlightControl**: Design & Programming +-- +-- @module SpawnStatic + + + +--- SPAWNSTATIC Class +-- @type SPAWNSTATIC +-- @extends Core.Base#BASE + + +--- # SPAWNSTATIC class, extends @{Base#BASE} +-- +-- The SPAWNSTATIC class allows to spawn dynamically new @{Static}s. +-- +-- There are two modes how SPAWNSTATIC can spawn: +-- +-- * Through creating a copy of an existing Template @{Static} as defined in the Mission Editor (ME). +-- * Through the provision of the type name of the Static. +-- +-- Spawned @{Static}s get **the same name** as the name of the Template Static, +-- or gets the given name when a Static Type is used. +-- Newly spawned @{Static}s will get the following naming structure at run-time: +-- +-- * Spawned @{Static}s will have the name _StaticName_#_nnn_, where _StaticName_ is the name of the **Template Static**, +-- and _nnn_ is a **counter from 0 to 99999**. +-- +-- +-- ## SPAWNSTATIC construction methods +-- +-- Create a new SPAWNSTATIC object with the @{#SPAWNSTATIC.NewFromStatic}() or the @{#SPAWNSTATIC.NewFromType}() methods: +-- +-- * @{#SPAWNSTATIC.NewFromStatic}(): Creates a new SPAWNSTATIC object given a name that is used as the base of the naming of each spawned Static. +-- * @{#SPAWNSTATIC.NewFromType}(): Creates a new SPAWNSTATIC object given a type name and a name to be given when spawned. +-- +-- ## SPAWNSTATIC **Init**ialization methods +-- +-- A spawn object will behave differently based on the usage of **initialization** methods, which all start with the **Init** prefix: +-- +-- * @{#SPAWNSTATIC.InitRandomizePosition}(): Randomizes the position of @{Static}s that are spawned within a **radius band**, given an Outer and Inner radius, from the point that the spawn happens. +-- * @{#SPAWNSTATIC.InitRandomizeZones}(): Randomizes the spawning between a predefined list of @{Zone}s that are declared using this function. Each zone can be given a probability factor. +-- +-- ## SPAWNSTATIC **Spawn** methods +-- +-- Groups can be spawned at different times and methods: +-- +-- * @{#SPAWNSTATIC.SpawnInZone}(): Spawn a new group in a @{Zone}. +-- * @{#SPAWNSTATIC.SpawnFromVec3}(): Spawn a new group from a Vec3 coordinate. (The group will can be spawned at a point in the air). +-- * @{#SPAWNSTATIC.SpawnFromVec2}(): Spawn a new group from a Vec2 coordinate. (The group will be spawned at land height ). +-- * @{#SPAWNSTATIC.SpawnFromStatic}(): Spawn a new group from a structure, taking the position of a @{Static}. +-- * @{#SPAWNSTATIC.SpawnFromUnit}(): Spawn a new group taking the position of a @{Unit}. +-- +-- @field #SPAWNSTATIC SPAWNSTATIC +-- +SPAWNSTATIC = { + ClassName = "SPAWNSTATIC", +} + + +--- @type SPAWNSTATIC.SpawnZoneTable +-- @list SpawnZone + + +--- Creates the main object to spawn a @{Static} defined in the ME. +-- @param #SPAWNSTATIC self +-- @param #string SpawnTemplatePrefix is the name of the Group in the ME that defines the Template. Each new group will have the name starting with SpawnTemplatePrefix. +-- @return #SPAWNSTATIC +function SPAWNSTATIC:NewFromStatic( SpawnTemplatePrefix, CountryID ) + local self = BASE:Inherit( self, BASE:New() ) -- #SPAWNSTATIC + self:F( { SpawnTemplatePrefix } ) + + local TemplateStatic = StaticObject.getByName( SpawnTemplatePrefix ) + if TemplateStatic then + self.SpawnTemplatePrefix = SpawnTemplatePrefix + self.CountryID = CountryID + self.SpawnIndex = 0 + else + error( "SPAWNSTATIC:New: There is no group declared in the mission editor with SpawnTemplatePrefix = '" .. SpawnTemplatePrefix .. "'" ) + end + + self:SetEventPriority( 5 ) + + return self +end + +--- Creates the main object to spawn a @{Static} based on a type name. +-- @param #SPAWNSTATIC self +-- @param #string SpawnTypeName is the name of the type. +-- @return #SPAWNSTATIC +function SPAWNSTATIC:NewFromType( SpawnTypeName, SpawnShapeName, SpawnCategory, CountryID ) + local self = BASE:Inherit( self, BASE:New() ) -- #SPAWNSTATIC + self:F( { SpawnTypeName } ) + + self.SpawnTypeName = SpawnTypeName + self.CountryID = CountryID + self.SpawnIndex = 0 + + self:SetEventPriority( 5 ) + + return self +end + + +--- Creates a new @{Static} from a POINT_VEC2. +-- @param #SPAWNSTATIC self +-- @param #string SpawnTypeName is the name of the type. +-- @return #SPAWNSTATIC +function SPAWNSTATIC:SpawnFromPointVec2( PointVec2, BaseName ) + self:F( { PointVec2, BaseName } ) + + local CountryName = _DATABASE.COUNTRY_NAME[self.CountryID] + + local StaticTemplate + + local Tire = { + ["country"] = CountryName, + ["category"] = "Fortifications", + ["canCargo"] = false, + ["shape_name"] = "H-tyre_B_WF", + ["type"] = "Black_Tyre_WF", + --["unitId"] = Angle + 10000, + ["y"] = Point.y, + ["x"] = Point.x, + ["name"] = string.format( "%s-Tire #%0d", self:GetName(), Angle ), + ["heading"] = 0, + } -- end of ["group"] + + local Group = coalition.addStaticObject( CountryID, Tire ) + + return self +end + + From 2b622c0a02d04921d6f98dbe18561849288b1b79 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Sun, 9 Apr 2017 07:53:34 +0200 Subject: [PATCH 2/2] Progress --- Moose Development/Moose/Core/Database.lua | 70 ++++++++++++++++++-- Moose Development/Moose/Core/SpawnStatic.lua | 62 +++++++++-------- Moose Development/Moose/Core/Zone.lua | 2 +- Moose Mission Setup/Moose.files | 1 + Moose Mission Setup/Moose.lua | 3 +- 5 files changed, 101 insertions(+), 37 deletions(-) diff --git a/Moose Development/Moose/Core/Database.lua b/Moose Development/Moose/Core/Database.lua index 3e9eea077..bdf743d0d 100644 --- a/Moose Development/Moose/Core/Database.lua +++ b/Moose Development/Moose/Core/Database.lua @@ -44,6 +44,7 @@ DATABASE = { Templates = { Units = {}, Groups = {}, + Statics = {}, ClientsByName = {}, ClientsByID = {}, }, @@ -282,7 +283,7 @@ function DATABASE:Spawn( SpawnTemplate ) SpawnTemplate.CountryID = nil SpawnTemplate.CategoryID = nil - self:_RegisterTemplate( SpawnTemplate, SpawnCoalitionID, SpawnCategoryID, SpawnCountryID ) + self:_RegisterGroupTemplate( SpawnTemplate, SpawnCoalitionID, SpawnCategoryID, SpawnCountryID ) self:T3( SpawnTemplate ) coalition.addGroup( SpawnCountryID, SpawnCategoryID, SpawnTemplate ) @@ -318,7 +319,7 @@ end -- @param #DATABASE self -- @param #table GroupTemplate -- @return #DATABASE self -function DATABASE:_RegisterTemplate( GroupTemplate, CoalitionID, CategoryID, CountryID ) +function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionID, CategoryID, CountryID ) local GroupTemplateName = env.getValueDictByKey(GroupTemplate.name) @@ -396,6 +397,54 @@ function DATABASE:GetGroupTemplate( GroupName ) return GroupTemplate end +--- Private method that registers new Static Templates within the DATABASE Object. +-- @param #DATABASE self +-- @param #table GroupTemplate +-- @return #DATABASE self +function DATABASE:_RegisterStaticTemplate( StaticTemplate, CoalitionID, CategoryID, CountryID ) + + local TraceTable = {} + + 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] + self.Templates.Statics[StaticTemplateName].CategoryID = CategoryID + self.Templates.Statics[StaticTemplateName].CoalitionID = CoalitionID + self.Templates.Statics[StaticTemplateName].CountryID = CountryID + + + TraceTable[#TraceTable+1] = "Static" + TraceTable[#TraceTable+1] = self.Templates.Statics[StaticTemplateName].GroupName + + TraceTable[#TraceTable+1] = "Coalition" + TraceTable[#TraceTable+1] = self.Templates.Statics[StaticTemplateName].CoalitionID + TraceTable[#TraceTable+1] = "Category" + TraceTable[#TraceTable+1] = self.Templates.Statics[StaticTemplateName].CategoryID + TraceTable[#TraceTable+1] = "Country" + TraceTable[#TraceTable+1] = self.Templates.Statics[StaticTemplateName].CountryID + + self:E( TraceTable ) +end + + +--- @param #DATABASE self +function DATABASE:GetStaticUnitTemplate( StaticName ) + local StaticTemplate = self.Templates.Statics[StaticName].UnitTemplate + StaticTemplate.SpawnCoalitionID = self.Templates.Statics[StaticName].CoalitionID + StaticTemplate.SpawnCategoryID = self.Templates.Statics[StaticName].CategoryID + StaticTemplate.SpawnCountryID = self.Templates.Statics[StaticName].CountryID + return StaticTemplate +end + + function DATABASE:GetGroupNameFromUnitName( UnitName ) return self.Templates.Units[UnitName].GroupName end @@ -725,7 +774,7 @@ function DATABASE:ForEachClient( IteratorFunction, ... ) return self end - +--- @param #DATABASE self function DATABASE:_RegisterTemplates() self:F2() @@ -781,11 +830,18 @@ function DATABASE:_RegisterTemplates() --self.Units[coa_name][countryName][category] = {} - for group_num, GroupTemplate in pairs(obj_type_data.group) do + for group_num, Template in pairs(obj_type_data.group) do - if GroupTemplate and GroupTemplate.units and type(GroupTemplate.units) == 'table' then --making sure again- this is a valid group - self:_RegisterTemplate( - GroupTemplate, + 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 + ) + else + self:_RegisterStaticTemplate( + Template, CoalitionSide, _DATABASECategory[string.lower(CategoryName)], CountryID diff --git a/Moose Development/Moose/Core/SpawnStatic.lua b/Moose Development/Moose/Core/SpawnStatic.lua index a891cf2b3..9e744d971 100644 --- a/Moose Development/Moose/Core/SpawnStatic.lua +++ b/Moose Development/Moose/Core/SpawnStatic.lua @@ -43,8 +43,7 @@ ---- SPAWNSTATIC Class --- @type SPAWNSTATIC +--- @type SPAWNSTATIC -- @extends Core.Base#BASE @@ -72,13 +71,6 @@ -- * @{#SPAWNSTATIC.NewFromStatic}(): Creates a new SPAWNSTATIC object given a name that is used as the base of the naming of each spawned Static. -- * @{#SPAWNSTATIC.NewFromType}(): Creates a new SPAWNSTATIC object given a type name and a name to be given when spawned. -- --- ## SPAWNSTATIC **Init**ialization methods --- --- A spawn object will behave differently based on the usage of **initialization** methods, which all start with the **Init** prefix: --- --- * @{#SPAWNSTATIC.InitRandomizePosition}(): Randomizes the position of @{Static}s that are spawned within a **radius band**, given an Outer and Inner radius, from the point that the spawn happens. --- * @{#SPAWNSTATIC.InitRandomizeZones}(): Randomizes the spawning between a predefined list of @{Zone}s that are declared using this function. Each zone can be given a probability factor. --- -- ## SPAWNSTATIC **Spawn** methods -- -- Groups can be spawned at different times and methods: @@ -142,31 +134,45 @@ end --- Creates a new @{Static} from a POINT_VEC2. -- @param #SPAWNSTATIC self --- @param #string SpawnTypeName is the name of the type. +-- @param Core.Point#POINT_VEC2 PointVec2 The 2D coordinate where to spawn the static. +-- @param #number Heading The heading of the static, which is a number in degrees from 0 to 360. +-- @param #string (optional) The name of the new static. -- @return #SPAWNSTATIC -function SPAWNSTATIC:SpawnFromPointVec2( PointVec2, BaseName ) - self:F( { PointVec2, BaseName } ) +function SPAWNSTATIC:SpawnFromPointVec2( PointVec2, Heading, NewName ) + self:F( { PointVec2, Heading, NewName } ) local CountryName = _DATABASE.COUNTRY_NAME[self.CountryID] - local StaticTemplate + local StaticTemplate = _DATABASE:GetStaticUnitTemplate( self.SpawnTemplatePrefix ) - local Tire = { - ["country"] = CountryName, - ["category"] = "Fortifications", - ["canCargo"] = false, - ["shape_name"] = "H-tyre_B_WF", - ["type"] = "Black_Tyre_WF", - --["unitId"] = Angle + 10000, - ["y"] = Point.y, - ["x"] = Point.x, - ["name"] = string.format( "%s-Tire #%0d", self:GetName(), Angle ), - ["heading"] = 0, - } -- end of ["group"] + StaticTemplate.x = PointVec2:GetLat() + StaticTemplate.y = PointVec2:GetLon() + + StaticTemplate.name = NewName or string.format("%s#%05d", self.SpawnTemplatePrefix, self.SpawnIndex ) + StaticTemplate.heading = ( Heading / 180 ) * math.pi + + StaticTemplate.CountryID = nil + StaticTemplate.CoalitionID = nil + StaticTemplate.CategoryID = nil + + local Static = coalition.addStaticObject( self.CountryID, StaticTemplate ) + + self.SpawnIndex = self.SpawnIndex + 1 - local Group = coalition.addStaticObject( CountryID, Tire ) - - return self + return Static end +--- Creates a new @{Static} from a @{Zone}. +-- @param #SPAWNSTATIC self +-- @param Core.Zone#ZONE_BASE Zone The Zone where to spawn the static. +-- @param #number Heading The heading of the static, which is a number in degrees from 0 to 360. +-- @param #string (optional) The name of the new static. +-- @return #SPAWNSTATIC +function SPAWNSTATIC:SpawnFromZone( Zone, Heading, NewName ) + self:F( { Zone, Heading, NewName } ) + + local Static = self:SpawnFromPointVec2( Zone:GetPointVec2(), Heading, NewName ) + + return Static +end diff --git a/Moose Development/Moose/Core/Zone.lua b/Moose Development/Moose/Core/Zone.lua index 432a360a7..8456729a8 100644 --- a/Moose Development/Moose/Core/Zone.lua +++ b/Moose Development/Moose/Core/Zone.lua @@ -615,7 +615,7 @@ end --- @type ZONE +--- @type ZONE -- @extends Core.Zone#ZONE_RADIUS diff --git a/Moose Mission Setup/Moose.files b/Moose Mission Setup/Moose.files index fac9aa46b..b8084a749 100644 --- a/Moose Mission Setup/Moose.files +++ b/Moose Mission Setup/Moose.files @@ -13,6 +13,7 @@ Core/Point.lua Core/Message.lua Core/Fsm.lua Core/Radio.lua +Core/SpawnStatic.lua Wrapper/Object.lua Wrapper/Identifiable.lua diff --git a/Moose Mission Setup/Moose.lua b/Moose Mission Setup/Moose.lua index 7fc7af669..b81506f59 100644 --- a/Moose Mission Setup/Moose.lua +++ b/Moose Mission Setup/Moose.lua @@ -1,5 +1,5 @@ env.info( '*** MOOSE DYNAMIC INCLUDE START *** ' ) -env.info( 'Moose Generation Timestamp: 20170405_0934' ) +env.info( 'Moose Generation Timestamp: 20170409_0720' ) local base = _G @@ -35,6 +35,7 @@ __Moose.Include( 'Core/Point.lua' ) __Moose.Include( 'Core/Message.lua' ) __Moose.Include( 'Core/Fsm.lua' ) __Moose.Include( 'Core/Radio.lua' ) +__Moose.Include( 'Core/SpawnStatic.lua' ) __Moose.Include( 'Wrapper/Object.lua' ) __Moose.Include( 'Wrapper/Identifiable.lua' ) __Moose.Include( 'Wrapper/Positionable.lua' )