From 465c39529475f05de19c85299931e5c8ff43cf8a Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Thu, 18 Apr 2024 14:41:29 +0200 Subject: [PATCH 01/15] SPAWN - Allow setting of "hidden" options --- Moose Development/Moose/Core/Spawn.lua | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/Moose Development/Moose/Core/Spawn.lua b/Moose Development/Moose/Core/Spawn.lua index 8be96a7e7..e9747fc9b 100644 --- a/Moose Development/Moose/Core/Spawn.lua +++ b/Moose Development/Moose/Core/Spawn.lua @@ -1467,6 +1467,30 @@ do -- Delay methods end -- Delay methods +--- Hide the group on the map view (visible to game master slots!). +-- @param #SPAWN self +-- @return #SPAWN The SPAWN object +function SPAWN:InitHiddenOnMap() + self.SpawnHiddenOnMap = true + return self +end + +--- Hide the group on MFDs (visible to game master slots!). +-- @param #SPAWN self +-- @return #SPAWN The SPAWN object +function SPAWN:InitHiddenOnMFD() + self.SpawnHiddenOnMFD = true + return self +end + +--- Hide the group on planner (visible to game master slots!). +-- @param #SPAWN self +-- @return #SPAWN The SPAWN object +function SPAWN:InitHiddenOnPlanner() + self.SpawnHiddenOnPlanner = true + return self +end + --- Will spawn a group based on the internal index. -- Note: This method uses the global _DATABASE object (an instance of @{Core.Database#DATABASE}), which contains ALL initial and new spawned objects in MOOSE. -- @param #SPAWN self @@ -1740,7 +1764,22 @@ function SPAWN:SpawnWithIndex( SpawnIndex, NoBirth ) if self.SpawnInitModu then SpawnTemplate.modulation = self.SpawnInitModu end + + -- hiding options + if self.SpawnHiddenOnPlanner then + SpawnTemplate.hiddenOnPlanner=true + end + if self.SpawnHiddenOnMFD then + SpawnTemplate.hiddenOnMFD=true + end + + if self.SpawnHiddenOnMap then + SpawnTemplate.hidden=true + end + + self:I(SpawnTemplate) + -- Set country, coalition and category. SpawnTemplate.CategoryID = self.SpawnInitCategory or SpawnTemplate.CategoryID SpawnTemplate.CountryID = self.SpawnInitCountry or SpawnTemplate.CountryID From 616690391c8dcb511b009455c4be05971255c0d2 Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Thu, 18 Apr 2024 14:51:41 +0200 Subject: [PATCH 02/15] SADL/STN conversion fix --- Moose Development/Moose/Core/Database.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Moose Development/Moose/Core/Database.lua b/Moose Development/Moose/Core/Database.lua index cbfa62555..bdd3fcaae 100644 --- a/Moose Development/Moose/Core/Database.lua +++ b/Moose Development/Moose/Core/Database.lua @@ -1009,7 +1009,7 @@ function DATABASE:_RegisterGroupTemplate( GroupTemplate, CoalitionSide, Category self.Templates.Groups[GroupTemplateName].CategoryID = CategoryID self.Templates.Groups[GroupTemplateName].CoalitionID = CoalitionSide self.Templates.Groups[GroupTemplateName].CountryID = CountryID - + local UnitNames = {} for unit_num, UnitTemplate in pairs( GroupTemplate.units ) do @@ -1074,7 +1074,7 @@ end -- @param #string unitname Name of the associated unit. -- @return #number Octal function DATABASE:GetNextSTN(octal,unitname) - local first = UTILS.OctalToDecimal(octal) + local first = UTILS.OctalToDecimal(octal) or 0 if self.STNS[first] == unitname then return octal end local nextoctal = 77777 local found = false @@ -1111,7 +1111,7 @@ end -- @param #string unitname Name of the associated unit. -- @return #number Octal function DATABASE:GetNextSADL(octal,unitname) - local first = UTILS.OctalToDecimal(octal) + local first = UTILS.OctalToDecimal(octal) or 0 if self.SADL[first] == unitname then return octal end local nextoctal = 7777 local found = false @@ -2081,7 +2081,7 @@ 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) else From b761078c188e1bfec4eee19947a2487ad9ffcd07 Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Thu, 18 Apr 2024 18:40:59 +0200 Subject: [PATCH 03/15] XXX --- Moose Development/Moose/Core/Database.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Moose Development/Moose/Core/Database.lua b/Moose Development/Moose/Core/Database.lua index bdd3fcaae..8f031fdd0 100644 --- a/Moose Development/Moose/Core/Database.lua +++ b/Moose Development/Moose/Core/Database.lua @@ -1685,7 +1685,7 @@ function DATABASE:_EventOnPlayerLeaveUnit( Event ) if Event.IniObjectCategory == 1 then -- Try to get the player name. This can be buggy for multicrew aircraft! - local PlayerName = Event.IniUnit:GetPlayerName() or FindPlayerName(Event.IniUnitName) + local PlayerName = Event.IniPlayerName or Event.IniUnit:GetPlayerName() or FindPlayerName(Event.IniUnitName) if PlayerName then From abc26b1e5cc0eab14ea73c3d6a65b1873fac80eb Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Fri, 19 Apr 2024 11:33:15 +0200 Subject: [PATCH 04/15] #CSAR Add'l logging --- Moose Development/Moose/Ops/CSAR.lua | 68 +++++++++++++++++++--------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/Moose Development/Moose/Ops/CSAR.lua b/Moose Development/Moose/Ops/CSAR.lua index f1d366ca4..905798115 100644 --- a/Moose Development/Moose/Ops/CSAR.lua +++ b/Moose Development/Moose/Ops/CSAR.lua @@ -30,8 +30,8 @@ -- @module Ops.CSAR -- @image OPS_CSAR.jpg --- Date: May 2023 --- Last: Update Dec 2024 +--- +-- Last Update April 2024 ------------------------------------------------------------------------- --- **CSAR** class, extends Core.Base#BASE, Core.Fsm#FSM @@ -294,7 +294,7 @@ CSAR.AircraftType["MH-60R"] = 10 --- CSAR class version. -- @field #string version -CSAR.version="1.0.20" +CSAR.version="1.0.21" ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- ToDo list @@ -463,7 +463,7 @@ function CSAR:New(Coalition, Template, Alias) self.SRSModulation = radio.modulation.AM -- modulation self.SRSport = 5002 -- port self.SRSCulture = "en-GB" - self.SRSVoice = nil + self.SRSVoice = MSRS.Voices.Google.Standard.en_GB_Standard_B self.SRSGPathToCredentials = nil self.SRSVolume = 1.0 -- volume 0.0 to 1.0 self.SRSGender = "male" -- male or female @@ -1190,7 +1190,7 @@ function CSAR:_EventHandler(EventData) if _place:GetCoalition() == self.coalition or _place:GetCoalition() == coalition.side.NEUTRAL then self:__Landed(2,_event.IniUnitName, _place) - self:_ScheduledSARFlight(_event.IniUnitName,_event.IniGroupName,true) + self:_ScheduledSARFlight(_event.IniUnitName,_event.IniGroupName,true,true) else self:T(string.format("Airfield %d, Unit %d", _place:GetCoalition(), _unit:GetCoalition())) end @@ -1529,7 +1529,7 @@ function CSAR:_CheckCloseWoundedGroup(_distance, _heliUnit, _heliName, _woundedG local _reset = true if (_distance < 500) then - + self:T(self.lid .. "[Pickup Debug] Helo closer than 500m: ".._lookupKeyHeli) if self.heliCloseMessage[_lookupKeyHeli] == nil then if self.autosmoke == true then self:_DisplayMessageToSAR(_heliUnit, string.format("%s: %s. You\'re close now! Land or hover at the smoke.", self:_GetCustomCallSign(_heliName), _pilotName), self.messageTime,false,true) @@ -1538,14 +1538,16 @@ function CSAR:_CheckCloseWoundedGroup(_distance, _heliUnit, _heliName, _woundedG end self.heliCloseMessage[_lookupKeyHeli] = true end - + self:T(self.lid .. "[Pickup Debug] Checking landed vs Hover for ".._lookupKeyHeli) -- have we landed close enough? if not _heliUnit:InAir() then - + self:T(self.lid .. "[Pickup Debug] Helo landed: ".._lookupKeyHeli) if self.pilotRuntoExtractPoint == true then if (_distance < self.extractDistance) then local _time = self.landedStatus[_lookupKeyHeli] + self:T(self.lid .. "[Pickup Debug] Check pilot running or arrived ".._lookupKeyHeli) if _time == nil then + self:T(self.lid .. "[Pickup Debug] Pilot running not arrived yet ".._lookupKeyHeli) self.landedStatus[_lookupKeyHeli] = math.floor( (_distance - self.loadDistance) / 3.6 ) _time = self.landedStatus[_lookupKeyHeli] _woundedGroup:OptionAlarmStateGreen() @@ -1556,11 +1558,15 @@ function CSAR:_CheckCloseWoundedGroup(_distance, _heliUnit, _heliName, _woundedG self.landedStatus[_lookupKeyHeli] = _time end --if _time <= 0 or _distance < self.loadDistance then + self:T(self.lid .. "[Pickup Debug] Pilot close enough? ".._lookupKeyHeli) if _distance < self.loadDistance + 5 or _distance <= 13 then + self:T(self.lid .. "[Pickup Debug] Pilot close enough - YES ".._lookupKeyHeli) if self.pilotmustopendoors and (self:_IsLoadingDoorOpen(_heliName) == false) then self:_DisplayMessageToSAR(_heliUnit, "Open the door to let me in!", self.messageTime, true, true) + self:T(self.lid .. "[Pickup Debug] Door closed, try again next loop ".._lookupKeyHeli) return false else + self:T(self.lid .. "[Pickup Debug] Pick up Pilot ".._lookupKeyHeli) self.landedStatus[_lookupKeyHeli] = nil self:_PickupUnit(_heliUnit, _pilotName, _woundedGroup, _woundedGroupName) return true @@ -1568,28 +1574,32 @@ function CSAR:_CheckCloseWoundedGroup(_distance, _heliUnit, _heliName, _woundedG end end else + self:T(self.lid .. "[Pickup Debug] Helo landed, pilot NOT set to run to helo ".._lookupKeyHeli) if (_distance < self.loadDistance) then + self:T(self.lid .. "[Pickup Debug] Helo close enough, door check ".._lookupKeyHeli) if self.pilotmustopendoors and (self:_IsLoadingDoorOpen(_heliName) == false) then + self:T(self.lid .. "[Pickup Debug] Door closed, try again next loop ".._lookupKeyHeli) self:_DisplayMessageToSAR(_heliUnit, "Open the door to let me in!", self.messageTime, true, true) return false else + self:T(self.lid .. "[Pickup Debug] Pick up Pilot ".._lookupKeyHeli) self:_PickupUnit(_heliUnit, _pilotName, _woundedGroup, _woundedGroupName) return true end end end else - + self:T(self.lid .. "[Pickup Debug] Helo hovering".._lookupKeyHeli) local _unitsInHelicopter = self:_PilotsOnboard(_heliName) local _maxUnits = self.AircraftType[_heliUnit:GetTypeName()] if _maxUnits == nil then _maxUnits = self.max_units end - + self:T(self.lid .. "[Pickup Debug] Check capacity and close enough for winching ".._lookupKeyHeli) if _heliUnit:InAir() and _unitsInHelicopter + 1 <= _maxUnits then -- DONE - make variable if _distance < self.rescuehoverdistance then - + self:T(self.lid .. "[Pickup Debug] Helo hovering close enough ".._lookupKeyHeli) --check height! local leaderheight = _woundedLeader:GetHeight() if leaderheight < 0 then leaderheight = 0 end @@ -1597,7 +1607,7 @@ function CSAR:_CheckCloseWoundedGroup(_distance, _heliUnit, _heliName, _woundedG -- DONE - make variable if _height <= self.rescuehoverheight then - + self:T(self.lid .. "[Pickup Debug] Helo hovering low enough ".._lookupKeyHeli) local _time = self.hoverStatus[_lookupKeyHeli] if _time == nil then @@ -1607,22 +1617,28 @@ function CSAR:_CheckCloseWoundedGroup(_distance, _heliUnit, _heliName, _woundedG _time = self.hoverStatus[_lookupKeyHeli] - 10 self.hoverStatus[_lookupKeyHeli] = _time end - + self:T(self.lid .. "[Pickup Debug] Check hover timer ".._lookupKeyHeli) if _time > 0 then + self:T(self.lid .. "[Pickup Debug] Helo hovering not long enough ".._lookupKeyHeli) self:_DisplayMessageToSAR(_heliUnit, "Hovering above " .. _pilotName .. ". \n\nHold hover for " .. _time .. " seconds to winch them up. \n\nIf the countdown stops you\'re too far away!", self.messageTime, true) else + self:T(self.lid .. "[Pickup Debug] Helo hovering long enough - door check ".._lookupKeyHeli) if self.pilotmustopendoors and (self:_IsLoadingDoorOpen(_heliName) == false) then self:_DisplayMessageToSAR(_heliUnit, "Open the door to let me in!", self.messageTime, true, true) + self:T(self.lid .. "[Pickup Debug] Door closed, try again next loop ".._lookupKeyHeli) return false else self.hoverStatus[_lookupKeyHeli] = nil self:_PickupUnit(_heliUnit, _pilotName, _woundedGroup, _woundedGroupName) + self:T(self.lid .. "[Pickup Debug] Pilot picked up ".._lookupKeyHeli) return true end end _reset = false else + self:T(self.lid .. "[Pickup Debug] Helo hovering too high ".._lookupKeyHeli) self:_DisplayMessageToSAR(_heliUnit, "Too high to winch " .. _pilotName .. " \nReduce height and hover for 10 seconds!", self.messageTime, true,true) + self:T(self.lid .. "[Pickup Debug] Hovering too high, try again next loop ".._lookupKeyHeli) return false end end @@ -1647,7 +1663,8 @@ end -- @param #string heliname Heli name -- @param #string groupname Group name -- @param #boolean isairport If true, EVENT.Landing took place at an airport or FARP -function CSAR:_ScheduledSARFlight(heliname,groupname, isairport) +-- @param #boolean noreschedule If true, do not try to reschedule this is distances are not ok (coming from landing event) +function CSAR:_ScheduledSARFlight(heliname,groupname, isairport, noreschedule) self:T(self.lid .. " _ScheduledSARFlight") self:T({heliname,groupname}) local _heliUnit = self:_GetSARHeli(heliname) @@ -1667,20 +1684,29 @@ function CSAR:_ScheduledSARFlight(heliname,groupname, isairport) local _dist = self:_GetClosestMASH(_heliUnit) if _dist == -1 then - return + self:T(self.lid.."[Drop off debug] Check distance to MASH for "..heliname.." Distance can not be determined!") + return end - + + self:T(self.lid.."[Drop off debug] Check distance to MASH for "..heliname.." Distance km: "..math.floor(_dist/1000)) + if ( _dist < self.FARPRescueDistance or isairport ) and _heliUnit:InAir() == false then + self:T(self.lid.."[Drop off debug] Distance ok, door check") if self.pilotmustopendoors and self:_IsLoadingDoorOpen(heliname) == false then self:_DisplayMessageToSAR(_heliUnit, "Open the door to let me out!", self.messageTime, true, true) + self:T(self.lid.."[Drop off debug] Door closed, try again next loop") else + self:T(self.lid.."[Drop off debug] Rescued!") self:_RescuePilots(_heliUnit) return end end --queue up - self:__Returning(-5,heliname,_woundedGroupName, isairport) + if not noreschedule then + self:__Returning(5,heliname,_woundedGroupName, isairport) + self:ScheduleOnce(5,self._ScheduledSARFlight,self,heliname,groupname, isairport, noreschedule) + end return self end @@ -1752,7 +1778,7 @@ function CSAR:_DisplayMessageToSAR(_unit, _text, _time, _clear, _speak, _overrid _text = string.gsub(_text,"nm"," nautical miles") --self.msrs:SetVoice(self.SRSVoice) --self.SRSQueue:NewTransmission(_text,nil,self.msrs,nil,1) - self:I("Voice = "..self.SRSVoice) + --self:I("Voice = "..self.SRSVoice) self.SRSQueue:NewTransmission(_text,duration,self.msrs,tstart,2,subgroups,subtitle,subduration,self.SRSchannel,self.SRSModulation,gender,culture,self.SRSVoice,volume,label,coord) end return self @@ -1981,7 +2007,7 @@ end --- (Internal) Determine distance to closest MASH. -- @param #CSAR self -- @param Wrapper.Unit#UNIT _heli Helicopter #UNIT --- @retunr +-- @return #CSAR self function CSAR:_GetClosestMASH(_heli) self:T(self.lid .. " _GetClosestMASH") local _mashset = self.mash -- Core.Set#SET_GROUP @@ -2219,7 +2245,7 @@ function CSAR:_RefreshRadioBeacons() if self:_CountActiveDownedPilots() > 0 then local PilotTable = self.downedPilots for _,_pilot in pairs (PilotTable) do - self:T({_pilot}) + self:T({_pilot.name}) local pilot = _pilot -- #CSAR.DownedPilot local group = pilot.group local frequency = pilot.frequency or 0 -- thanks to @Thrud @@ -2501,7 +2527,7 @@ end -- @param #boolean IsAirport True if heli has landed on an AFB (from event land). function CSAR:onbeforeReturning(From, Event, To, Heliname, Woundedgroupname, IsAirPort) self:T({From, Event, To, Heliname, Woundedgroupname}) - self:_ScheduledSARFlight(Heliname,Woundedgroupname, IsAirPort) + --self:_ScheduledSARFlight(Heliname,Woundedgroupname, IsAirPort) return self end From 8b2237d18367651001b52467dd95c938e19af007 Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Fri, 19 Apr 2024 15:45:11 +0200 Subject: [PATCH 05/15] #CTLD small G fix --- Moose Development/Moose/Ops/CTLD.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Moose Development/Moose/Ops/CTLD.lua b/Moose Development/Moose/Ops/CTLD.lua index d55132fee..65db26ed8 100644 --- a/Moose Development/Moose/Ops/CTLD.lua +++ b/Moose Development/Moose/Ops/CTLD.lua @@ -2297,7 +2297,7 @@ end --return self else self.CargoCounter = self.CargoCounter + 1 - nearestGroup.ExtractTime = timer.GetTime() + nearestGroup.ExtractTime = timer.getTime() local loadcargotype = CTLD_CARGO:New(self.CargoCounter, Cargotype.Name, Cargotype.Templates, Cargotype.CargoType, true, true, Cargotype.CratesNeeded,nil,nil,Cargotype.PerCrateMass) self:T({cargotype=loadcargotype}) local running = math.floor(nearestDistance / 4)+10 -- time run to helo plus boarding From 2a4e242eb22a212077660e29dc1aca255dd71e40 Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Fri, 19 Apr 2024 15:54:21 +0200 Subject: [PATCH 06/15] #AI* minor fixes --- .../Moose/AI/AI_A2A_Dispatcher.lua | 40 +++++------ .../Moose/AI/AI_A2G_Dispatcher.lua | 70 +++++++++---------- Moose Development/Moose/AI/AI_Air.lua | 59 +++++++++------- .../Moose/AI/AI_Air_Dispatcher.lua | 44 ++++++------ Moose Development/Moose/AI/AI_Air_Engage.lua | 48 +++++++------ .../Moose/AI/AI_Air_Squadron.lua | 4 +- Moose Development/Moose/AI/AI_Cargo.lua | 4 +- .../Moose/AI/AI_Cargo_Dispatcher.lua | 4 +- Moose Development/Moose/AI/AI_Escort.lua | 2 +- .../Moose/AI/AI_Escort_Dispatcher.lua | 30 ++++---- Moose Development/Moose/AI/AI_Patrol.lua | 28 ++++---- 11 files changed, 172 insertions(+), 161 deletions(-) diff --git a/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua b/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua index a548e83d9..bbfaa868b 100644 --- a/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua +++ b/Moose Development/Moose/AI/AI_A2A_Dispatcher.lua @@ -1151,14 +1151,14 @@ do -- AI_A2A_DISPATCHER local AirbaseName = EventData.PlaceName -- The name of the airbase that was captured. - self:I( "Captured " .. AirbaseName ) + self:T( "Captured " .. AirbaseName ) -- Now search for all squadrons located at the airbase, and sanitize them. for SquadronName, Squadron in pairs( self.DefenderSquadrons ) do if Squadron.AirbaseName == AirbaseName then Squadron.ResourceCount = -999 -- The base has been captured, and the resources are eliminated. No more spawning. Squadron.Captured = true - self:I( "Squadron " .. SquadronName .. " captured." ) + self:T( "Squadron " .. SquadronName .. " captured." ) end end end @@ -1828,7 +1828,7 @@ do -- AI_A2A_DISPATCHER self:SetSquadronCapInterval( SquadronName, self.DefenderDefault.CapLimit, self.DefenderDefault.CapMinSeconds, self.DefenderDefault.CapMaxSeconds, 1 ) - self:I( { CAP = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, Zone, PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageAltType } } ) + self:T( { CAP = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, Zone, PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageAltType } } ) -- Add the CAP to the EWR network. @@ -2085,7 +2085,7 @@ do -- AI_A2A_DISPATCHER Intercept.EngageCeilingAltitude = EngageCeilingAltitude Intercept.EngageAltType = EngageAltType - self:I( { GCI = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) + self:T( { GCI = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) end --- Set squadron GCI. @@ -3000,17 +3000,17 @@ do -- AI_A2A_DISPATCHER for FriendlyDistance, AIFriendly in UTILS.spairs( DefenderFriendlies or {} ) do -- We only allow to ENGAGE targets as long as the Units on both sides are balanced. if AttackerCount > DefenderCount then - --self:I("***** AI_A2A_DISPATCHER:CountDefendersToBeEngaged() *****\nThis is supposed to be a UNIT:") + --self:T("***** AI_A2A_DISPATCHER:CountDefendersToBeEngaged() *****\nThis is supposed to be a UNIT:") if AIFriendly then local classname = AIFriendly.ClassName or "No Class Name" local unitname = AIFriendly.IdentifiableName or "No Unit Name" - --self:I("Class Name: " .. classname) - --self:I("Unit Name: " .. unitname) - --self:I({AIFriendly}) + --self:T("Class Name: " .. classname) + --self:T("Unit Name: " .. unitname) + --self:T({AIFriendly}) end local Friendly = nil if AIFriendly and AIFriendly:IsAlive() then - --self:I("AIFriendly alive, getting GROUP") + --self:T("AIFriendly alive, getting GROUP") Friendly = AIFriendly:GetGroup() -- Wrapper.Group#GROUP end @@ -3952,7 +3952,7 @@ end do - --- @type AI_A2A_GCICAP + -- @type AI_A2A_GCICAP -- @extends #AI_A2A_DISPATCHER --- Create an automatic air defence system for a coalition setting up GCI and CAP air defenses. @@ -4322,23 +4322,23 @@ do -- Setup squadrons - self:I( { Airbases = AirbaseNames } ) + self:T( { Airbases = AirbaseNames } ) - self:I( "Defining Templates for Airbases ..." ) + self:T( "Defining Templates for Airbases ..." ) for AirbaseID, AirbaseName in pairs( AirbaseNames ) do local Airbase = _DATABASE:FindAirbase( AirbaseName ) -- Wrapper.Airbase#AIRBASE local AirbaseName = Airbase:GetName() local AirbaseCoord = Airbase:GetCoordinate() local AirbaseZone = ZONE_RADIUS:New( "Airbase", AirbaseCoord:GetVec2(), 3000 ) local Templates = nil - self:I( { Airbase = AirbaseName } ) + self:T( { Airbase = AirbaseName } ) for TemplateID, Template in pairs( self.Templates:GetSet() ) do local Template = Template -- Wrapper.Group#GROUP local TemplateCoord = Template:GetCoordinate() if AirbaseZone:IsVec2InZone( TemplateCoord:GetVec2() ) then Templates = Templates or {} table.insert( Templates, Template:GetName() ) - self:I( { Template = Template:GetName() } ) + self:T( { Template = Template:GetName() } ) end end if Templates then @@ -4354,13 +4354,13 @@ do self.CAPTemplates:FilterPrefixes( CapPrefixes ) self.CAPTemplates:FilterOnce() - self:I( "Setting up CAP ..." ) + self:T( "Setting up CAP ..." ) for CAPID, CAPTemplate in pairs( self.CAPTemplates:GetSet() ) do local CAPZone = ZONE_POLYGON:New( CAPTemplate:GetName(), CAPTemplate ) -- Now find the closest airbase from the ZONE (start or center) local AirbaseDistance = 99999999 local AirbaseClosest = nil -- Wrapper.Airbase#AIRBASE - self:I( { CAPZoneGroup = CAPID } ) + self:T( { CAPZoneGroup = CAPID } ) for AirbaseID, AirbaseName in pairs( AirbaseNames ) do local Airbase = _DATABASE:FindAirbase( AirbaseName ) -- Wrapper.Airbase#AIRBASE local AirbaseName = Airbase:GetName() @@ -4368,7 +4368,7 @@ do local Squadron = self.DefenderSquadrons[AirbaseName] if Squadron then local Distance = AirbaseCoord:Get2DDistance( CAPZone:GetCoordinate() ) - self:I( { AirbaseDistance = Distance } ) + self:T( { AirbaseDistance = Distance } ) if Distance < AirbaseDistance then AirbaseDistance = Distance AirbaseClosest = Airbase @@ -4376,7 +4376,7 @@ do end end if AirbaseClosest then - self:I( { CAPAirbase = AirbaseClosest:GetName() } ) + self:T( { CAPAirbase = AirbaseClosest:GetName() } ) self:SetSquadronCap( AirbaseClosest:GetName(), CAPZone, 6000, 10000, 500, 800, 800, 1200, "RADIO" ) self:SetSquadronCapInterval( AirbaseClosest:GetName(), CapLimit, 300, 600, 1 ) end @@ -4384,14 +4384,14 @@ do -- Setup GCI. -- GCI is setup for all Squadrons. - self:I( "Setting up GCI ..." ) + self:T( "Setting up GCI ..." ) for AirbaseID, AirbaseName in pairs( AirbaseNames ) do local Airbase = _DATABASE:FindAirbase( AirbaseName ) -- Wrapper.Airbase#AIRBASE local AirbaseName = Airbase:GetName() local Squadron = self.DefenderSquadrons[AirbaseName] self:F( { Airbase = AirbaseName } ) if Squadron then - self:I( { GCIAirbase = AirbaseName } ) + self:T( { GCIAirbase = AirbaseName } ) self:SetSquadronGci( AirbaseName, 800, 1200 ) end end diff --git a/Moose Development/Moose/AI/AI_A2G_Dispatcher.lua b/Moose Development/Moose/AI/AI_A2G_Dispatcher.lua index 1dad17759..451414f7f 100644 --- a/Moose Development/Moose/AI/AI_A2G_Dispatcher.lua +++ b/Moose Development/Moose/AI/AI_A2G_Dispatcher.lua @@ -904,14 +904,14 @@ do -- AI_A2G_DISPATCHER -- @type AI_A2G_DISPATCHER.DefenseCoordinates -- @map <#string,Core.Point#COORDINATE> A list of all defense coordinates mapped per defense coordinate name. - --- @field #AI_A2G_DISPATCHER.DefenseCoordinates DefenseCoordinates + -- @field #AI_A2G_DISPATCHER.DefenseCoordinates DefenseCoordinates AI_A2G_DISPATCHER.DefenseCoordinates = {} --- Enumerator for spawns at airbases. -- @type AI_A2G_DISPATCHER.Takeoff -- @extends Wrapper.Group#GROUP.Takeoff - --- @field #AI_A2G_DISPATCHER.Takeoff Takeoff + -- @field #AI_A2G_DISPATCHER.Takeoff Takeoff AI_A2G_DISPATCHER.Takeoff = GROUP.Takeoff --- Defines Landing location. @@ -942,7 +942,7 @@ do -- AI_A2G_DISPATCHER -- @type AI_A2G_DISPATCHER.DefenseQueue -- @list<#AI_A2G_DISPATCHER.DefenseQueueItem> DefenseQueueItem A list of all defenses being queued ... - --- @field #AI_A2G_DISPATCHER.DefenseQueue DefenseQueue + -- @field #AI_A2G_DISPATCHER.DefenseQueue DefenseQueue AI_A2G_DISPATCHER.DefenseQueue = {} --- Defense approach types. @@ -1136,7 +1136,7 @@ do -- AI_A2G_DISPATCHER end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_DISPATCHER:onafterStart( From, Event, To ) self:GetParent( self ).onafterStart( self, From, Event, To ) @@ -1147,7 +1147,7 @@ do -- AI_A2G_DISPATCHER for Resource = 1, DefenderSquadron.ResourceCount or 0 do self:ResourcePark( DefenderSquadron ) end - self:I( "Parked resources for squadron " .. DefenderSquadron.Name ) + self:T( "Parked resources for squadron " .. DefenderSquadron.Name ) end end @@ -1201,7 +1201,7 @@ do -- AI_A2G_DISPATCHER end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_DISPATCHER:ResourcePark( DefenderSquadron ) local TemplateID = math.random( 1, #DefenderSquadron.Spawn ) local Spawn = DefenderSquadron.Spawn[ TemplateID ] -- Core.Spawn#SPAWN @@ -1218,33 +1218,33 @@ do -- AI_A2G_DISPATCHER end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_A2G_DISPATCHER:OnEventBaseCaptured( EventData ) local AirbaseName = EventData.PlaceName -- The name of the airbase that was captured. - self:I( "Captured " .. AirbaseName ) + self:T( "Captured " .. AirbaseName ) -- Now search for all squadrons located at the airbase, and sanitize them. for SquadronName, Squadron in pairs( self.DefenderSquadrons ) do if Squadron.AirbaseName == AirbaseName then Squadron.ResourceCount = -999 -- The base has been captured, and the resources are eliminated. No more spawning. Squadron.Captured = true - self:I( "Squadron " .. SquadronName .. " captured." ) + self:T( "Squadron " .. SquadronName .. " captured." ) end end end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_A2G_DISPATCHER:OnEventCrashOrDead( EventData ) self.Detection:ForgetDetectedUnit( EventData.IniUnitName ) end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_A2G_DISPATCHER:OnEventLand( EventData ) self:F( "Landed" ) @@ -1261,7 +1261,7 @@ do -- AI_A2G_DISPATCHER self:RemoveDefenderFromSquadron( Squadron, Defender ) end DefenderUnit:Destroy() - self:ResourcePark( Squadron, Defender ) + self:ResourcePark( Squadron ) return end if DefenderUnit:GetLife() ~= DefenderUnit:GetLife0() then @@ -1273,7 +1273,7 @@ do -- AI_A2G_DISPATCHER end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_A2G_DISPATCHER:OnEventEngineShutdown( EventData ) local DefenderUnit = EventData.IniUnit @@ -1289,7 +1289,7 @@ do -- AI_A2G_DISPATCHER self:RemoveDefenderFromSquadron( Squadron, Defender ) end DefenderUnit:Destroy() - self:ResourcePark( Squadron, Defender ) + self:ResourcePark( Squadron ) end end end @@ -1297,7 +1297,7 @@ do -- AI_A2G_DISPATCHER do -- Manage the defensive behaviour - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self -- @param #string DefenseCoordinateName The name of the coordinate to be defended by A2G defenses. -- @param Core.Point#COORDINATE DefenseCoordinate The coordinate to be defended by A2G defenses. function AI_A2G_DISPATCHER:AddDefenseCoordinate( DefenseCoordinateName, DefenseCoordinate ) @@ -1305,19 +1305,19 @@ do -- AI_A2G_DISPATCHER end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_DISPATCHER:SetDefenseReactivityLow() self.DefenseReactivity = 0.05 end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_DISPATCHER:SetDefenseReactivityMedium() self.DefenseReactivity = 0.15 end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_DISPATCHER:SetDefenseReactivityHigh() self.DefenseReactivity = 0.5 end @@ -1351,14 +1351,14 @@ do -- AI_A2G_DISPATCHER -- 1. the **distance of the closest airbase to target**, being smaller than the **Defend Radius**. -- 2. the **distance to any defense reference point**. -- - -- The **default** defense radius is defined as **400000** or **40km**. Override the default defense radius when the era of the warfare is early, or, + -- The **default** defense radius is defined as **40000** or **40km**. Override the default defense radius when the era of the warfare is early, or, -- when you don't want to let the AI_A2G_DISPATCHER react immediately when a certain border or area is not being crossed. -- -- Use the method @{#AI_A2G_DISPATCHER.SetDefendRadius}() to set a specific defend radius for all squadrons, -- **the Defense Radius is defined for ALL squadrons which are operational.** -- -- @param #AI_A2G_DISPATCHER self - -- @param #number DefenseRadius (Optional, Default = 200000) The defense radius to engage detected targets from the nearest capable and available squadron airbase. + -- @param #number DefenseRadius (Optional, Default = 20000) The defense radius to engage detected targets from the nearest capable and available squadron airbase. -- @return #AI_A2G_DISPATCHER -- @usage -- @@ -1373,7 +1373,7 @@ do -- AI_A2G_DISPATCHER -- function AI_A2G_DISPATCHER:SetDefenseRadius( DefenseRadius ) - self.DefenseRadius = DefenseRadius or 100000 + self.DefenseRadius = DefenseRadius or 40000 self.Detection:SetAcceptRange( self.DefenseRadius ) @@ -1868,7 +1868,7 @@ do -- AI_A2G_DISPATCHER end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self -- @param #string SquadronName The squadron name. -- @param #number TakeoffInterval Only Takeoff new units each specified interval in seconds in 10 seconds steps. -- @usage @@ -2144,7 +2144,7 @@ do -- AI_A2G_DISPATCHER Sead.EngageAltType = EngageAltType Sead.Defend = true - self:I( { SEAD = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) + self:T( { SEAD = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) return self end @@ -2234,7 +2234,7 @@ do -- AI_A2G_DISPATCHER self:SetSquadronPatrolInterval( SquadronName, self.DefenderDefault.PatrolLimit, self.DefenderDefault.PatrolMinSeconds, self.DefenderDefault.PatrolMaxSeconds, 1, "SEAD" ) - self:I( { SEAD = { Zone:GetName(), PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) + self:T( { SEAD = { Zone:GetName(), PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) end @@ -2295,7 +2295,7 @@ do -- AI_A2G_DISPATCHER Cas.EngageAltType = EngageAltType Cas.Defend = true - self:I( { CAS = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) + self:T( { CAS = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) return self end @@ -2385,7 +2385,7 @@ do -- AI_A2G_DISPATCHER self:SetSquadronPatrolInterval( SquadronName, self.DefenderDefault.PatrolLimit, self.DefenderDefault.PatrolMinSeconds, self.DefenderDefault.PatrolMaxSeconds, 1, "CAS" ) - self:I( { CAS = { Zone:GetName(), PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) + self:T( { CAS = { Zone:GetName(), PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) end @@ -2446,7 +2446,7 @@ do -- AI_A2G_DISPATCHER Bai.EngageAltType = EngageAltType Bai.Defend = true - self:I( { BAI = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) + self:T( { BAI = { SquadronName, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) return self end @@ -2536,7 +2536,7 @@ do -- AI_A2G_DISPATCHER self:SetSquadronPatrolInterval( SquadronName, self.DefenderDefault.PatrolLimit, self.DefenderDefault.PatrolMinSeconds, self.DefenderDefault.PatrolMaxSeconds, 1, "BAI" ) - self:I( { BAI = { Zone:GetName(), PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) + self:T( { BAI = { Zone:GetName(), PatrolMinSpeed, PatrolMaxSpeed, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolAltType, EngageMinSpeed, EngageMaxSpeed, EngageFloorAltitude, EngageCeilingAltitude, EngageAltType } } ) end @@ -3369,7 +3369,7 @@ do -- AI_A2G_DISPATCHER end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_DISPATCHER:AddDefenderToSquadron( Squadron, Defender, Size ) self.Defenders = self.Defenders or {} local DefenderName = Defender:GetName() @@ -3380,7 +3380,7 @@ do -- AI_A2G_DISPATCHER self:F( { DefenderName = DefenderName, SquadronResourceCount = Squadron.ResourceCount } ) end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_DISPATCHER:RemoveDefenderFromSquadron( Squadron, Defender ) self.Defenders = self.Defenders or {} local DefenderName = Defender:GetName() @@ -3796,7 +3796,7 @@ do -- AI_A2G_DISPATCHER Dispatcher:ClearDefenderTaskTarget( DefenderGroup ) end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_Fsm:onafterLostControl( DefenderGroup, From, Event, To ) self:F({"LostControl", DefenderGroup:GetName()}) self:GetParent(self).onafterHome( self, DefenderGroup, From, Event, To ) @@ -3813,7 +3813,7 @@ do -- AI_A2G_DISPATCHER end end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_Fsm:onafterHome( DefenderGroup, From, Event, To, Action ) self:F({"Home", DefenderGroup:GetName()}) self:GetParent(self).onafterHome( self, DefenderGroup, From, Event, To ) @@ -3894,7 +3894,7 @@ do -- AI_A2G_DISPATCHER local Squadron = Dispatcher:GetSquadronFromDefender( DefenderGroup ) if Squadron then - local FirstUnit = AttackSetUnit:GetFirst() + local FirstUnit = AttackSetUnit:GetRandomSurely() local Coordinate = FirstUnit:GetCoordinate() -- Core.Point#COORDINATE if self.SetSendPlayerMessages then Dispatcher:MessageToPlayers( Squadron, DefenderName .. ", on route to ground target at " .. Coordinate:ToStringA2G( DefenderGroup ), DefenderGroup ) @@ -3933,7 +3933,7 @@ do -- AI_A2G_DISPATCHER Dispatcher:ClearDefenderTaskTarget( DefenderGroup ) end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_Fsm:onafterLostControl( DefenderGroup, From, Event, To ) self:F({"Defender LostControl", DefenderGroup:GetName()}) self:GetParent(self).onafterHome( self, DefenderGroup, From, Event, To ) @@ -3950,7 +3950,7 @@ do -- AI_A2G_DISPATCHER end end - --- @param #AI_A2G_DISPATCHER self + -- @param #AI_A2G_DISPATCHER self function AI_A2G_Fsm:onafterHome( DefenderGroup, From, Event, To, Action ) self:F({"Defender Home", DefenderGroup:GetName()}) self:GetParent(self).onafterHome( self, DefenderGroup, From, Event, To ) diff --git a/Moose Development/Moose/AI/AI_Air.lua b/Moose Development/Moose/AI/AI_Air.lua index 07325c819..08c85e751 100644 --- a/Moose Development/Moose/AI/AI_Air.lua +++ b/Moose Development/Moose/AI/AI_Air.lua @@ -9,7 +9,7 @@ -- @module AI.AI_Air -- @image MOOSE.JPG ---- @type AI_AIR +-- @type AI_AIR -- @extends Core.Fsm#FSM_CONTROLLABLE --- The AI_AIR class implements the core functions to operate an AI @{Wrapper.Group}. @@ -264,7 +264,7 @@ function AI_AIR:New( AIGroup ) return self end ---- @param Wrapper.Group#GROUP self +-- @param Wrapper.Group#GROUP self -- @param Core.Event#EVENTDATA EventData function GROUP:OnEventTakeoff( EventData, Fsm ) Fsm:Takeoff() @@ -446,13 +446,13 @@ function AI_AIR:onafterReturn( Controllable, From, Event, To ) end ---- @param #AI_AIR self +-- @param #AI_AIR self function AI_AIR:onbeforeStatus() return self.CheckStatus end ---- @param #AI_AIR self +-- @param #AI_AIR self function AI_AIR:onafterStatus() if self.Controllable and self.Controllable:IsAlive() then @@ -465,7 +465,7 @@ function AI_AIR:onafterStatus() local DistanceFromHomeBase = self.HomeAirbase:GetCoordinate():Get2DDistance( self.Controllable:GetCoordinate() ) if DistanceFromHomeBase > self.DisengageRadius then - self:I( self.Controllable:GetName() .. " is too far from home base, RTB!" ) + self:T( self.Controllable:GetName() .. " is too far from home base, RTB!" ) self:Hold( 300 ) RTB = false end @@ -489,10 +489,10 @@ function AI_AIR:onafterStatus() if Fuel < self.FuelThresholdPercentage then if self.TankerName then - self:I( self.Controllable:GetName() .. " is out of fuel: " .. Fuel .. " ... Refuelling at Tanker!" ) + self:T( self.Controllable:GetName() .. " is out of fuel: " .. Fuel .. " ... Refuelling at Tanker!" ) self:Refuel() else - self:I( self.Controllable:GetName() .. " is out of fuel: " .. Fuel .. " ... RTB!" ) + self:T( self.Controllable:GetName() .. " is out of fuel: " .. Fuel .. " ... RTB!" ) local OldAIControllable = self.Controllable local OrbitTask = OldAIControllable:TaskOrbitCircle( math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude ), self.PatrolMinSpeed ) @@ -518,7 +518,7 @@ function AI_AIR:onafterStatus() -- Note that a group can consist of more units, so if one unit is damaged of a group, the mission may continue. -- The damaged unit will RTB due to DCS logic, and the others will continue to engage. if ( Damage / InitialLife ) < self.PatrolDamageThreshold then - self:I( self.Controllable:GetName() .. " is damaged: " .. Damage .. " ... RTB!" ) + self:T( self.Controllable:GetName() .. " is damaged: " .. Damage .. " ... RTB!" ) self:Damaged() RTB = true self:SetStatusOff() @@ -536,7 +536,7 @@ function AI_AIR:onafterStatus() if Damage ~= InitialLife then self:Damaged() else - self:I( self.Controllable:GetName() .. " control lost! " ) + self:T( self.Controllable:GetName() .. " control lost! " ) self:LostControl() end @@ -560,7 +560,7 @@ function AI_AIR:onafterStatus() end ---- @param Wrapper.Group#GROUP AIGroup +-- @param Wrapper.Group#GROUP AIGroup function AI_AIR.RTBRoute( AIGroup, Fsm ) AIGroup:F( { "AI_AIR.RTBRoute:", AIGroup:GetName() } ) @@ -571,7 +571,7 @@ function AI_AIR.RTBRoute( AIGroup, Fsm ) end ---- @param Wrapper.Group#GROUP AIGroup +-- @param Wrapper.Group#GROUP AIGroup function AI_AIR.RTBHold( AIGroup, Fsm ) AIGroup:F( { "AI_AIR.RTBHold:", AIGroup:GetName() } ) @@ -598,7 +598,7 @@ function AI_AIR:SetRTBSpeedFactors(MinFactor,MaxFactor) end ---- @param #AI_AIR self +-- @param #AI_AIR self -- @param Wrapper.Group#GROUP AIGroup function AI_AIR:onafterRTB( AIGroup, From, Event, To ) self:F( { AIGroup, From, Event, To } ) @@ -617,7 +617,10 @@ function AI_AIR:onafterRTB( AIGroup, From, Event, To ) --- Calculate the target route point. local FromCoord = AIGroup:GetCoordinate() + if not FromCoord then return end + local ToTargetCoord = self.HomeAirbase:GetCoordinate() -- coordinate is on land height(!) + local ToTargetVec3 = ToTargetCoord:GetVec3() ToTargetVec3.y = ToTargetCoord:GetLandHeight()+3000 -- let's set this 1000m/3000 feet above ground local ToTargetCoord2 = COORDINATE:NewFromVec3( ToTargetVec3 ) @@ -638,13 +641,13 @@ function AI_AIR:onafterRTB( AIGroup, From, Event, To ) local ToAirbaseCoord = ToTargetCoord2 if Distance < 5000 then - self:I( "RTB and near the airbase!" ) + self:T( "RTB and near the airbase!" ) self:Home() return end if not AIGroup:InAir() == true then - self:I( "Not anymore in the air, considered Home." ) + self:T( "Not anymore in the air, considered Home." ) self:Home() return end @@ -686,12 +689,12 @@ function AI_AIR:onafterRTB( AIGroup, From, Event, To ) end ---- @param #AI_AIR self +-- @param #AI_AIR self -- @param Wrapper.Group#GROUP AIGroup function AI_AIR:onafterHome( AIGroup, From, Event, To ) self:F( { AIGroup, From, Event, To } ) - self:I( "Group " .. self.Controllable:GetName() .. " ... Home! ( " .. self:GetState() .. " )" ) + self:T( "Group " .. self.Controllable:GetName() .. " ... Home! ( " .. self:GetState() .. " )" ) if AIGroup and AIGroup:IsAlive() then end @@ -700,15 +703,17 @@ end ---- @param #AI_AIR self +-- @param #AI_AIR self -- @param Wrapper.Group#GROUP AIGroup function AI_AIR:onafterHold( AIGroup, From, Event, To, HoldTime ) self:F( { AIGroup, From, Event, To } ) - self:I( "Group " .. self.Controllable:GetName() .. " ... Holding! ( " .. self:GetState() .. " )" ) + self:T( "Group " .. self.Controllable:GetName() .. " ... Holding! ( " .. self:GetState() .. " )" ) if AIGroup and AIGroup:IsAlive() then - local OrbitTask = AIGroup:TaskOrbitCircle( math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude ), self.PatrolMinSpeed ) + local Coordinate = AIGroup:GetCoordinate() + if Coordinate == nil then return end + local OrbitTask = AIGroup:TaskOrbitCircle( math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude ), self.PatrolMinSpeed, Coordinate ) local TimedOrbitTask = AIGroup:TaskControlled( OrbitTask, AIGroup:TaskCondition( nil, nil, nil, nil, HoldTime , nil ) ) local RTBTask = AIGroup:TaskFunction( "AI_AIR.RTBHold", self ) @@ -722,17 +727,17 @@ function AI_AIR:onafterHold( AIGroup, From, Event, To, HoldTime ) end ---- @param Wrapper.Group#GROUP AIGroup +-- @param Wrapper.Group#GROUP AIGroup function AI_AIR.Resume( AIGroup, Fsm ) - AIGroup:I( { "AI_AIR.Resume:", AIGroup:GetName() } ) + AIGroup:T( { "AI_AIR.Resume:", AIGroup:GetName() } ) if AIGroup:IsAlive() then Fsm:__RTB( Fsm.TaskDelay ) end end ---- @param #AI_AIR self +-- @param #AI_AIR self -- @param Wrapper.Group#GROUP AIGroup function AI_AIR:onafterRefuel( AIGroup, From, Event, To ) self:F( { AIGroup, From, Event, To } ) @@ -744,7 +749,7 @@ function AI_AIR:onafterRefuel( AIGroup, From, Event, To ) if Tanker and Tanker:IsAlive() and Tanker:IsAirPlane() then - self:I( "Group " .. self.Controllable:GetName() .. " ... Refuelling! State=" .. self:GetState() .. ", Refuelling tanker " .. self.TankerName ) + self:T( "Group " .. self.Controllable:GetName() .. " ... Refuelling! State=" .. self:GetState() .. ", Refuelling tanker " .. self.TankerName ) local RefuelRoute = {} @@ -798,13 +803,13 @@ end ---- @param #AI_AIR self +-- @param #AI_AIR self function AI_AIR:onafterDead() self:SetStatusOff() end ---- @param #AI_AIR self +-- @param #AI_AIR self -- @param Core.Event#EVENTDATA EventData function AI_AIR:OnCrash( EventData ) @@ -815,7 +820,7 @@ function AI_AIR:OnCrash( EventData ) end end ---- @param #AI_AIR self +-- @param #AI_AIR self -- @param Core.Event#EVENTDATA EventData function AI_AIR:OnEjection( EventData ) @@ -824,7 +829,7 @@ function AI_AIR:OnEjection( EventData ) end end ---- @param #AI_AIR self +-- @param #AI_AIR self -- @param Core.Event#EVENTDATA EventData function AI_AIR:OnPilotDead( EventData ) diff --git a/Moose Development/Moose/AI/AI_Air_Dispatcher.lua b/Moose Development/Moose/AI/AI_Air_Dispatcher.lua index 8d0bbd9cf..9e5939aa0 100644 --- a/Moose Development/Moose/AI/AI_Air_Dispatcher.lua +++ b/Moose Development/Moose/AI/AI_Air_Dispatcher.lua @@ -900,14 +900,14 @@ do -- AI_AIR_DISPATCHER -- @type AI_AIR_DISPATCHER.DefenseCoordinates -- @map <#string,Core.Point#COORDINATE> A list of all defense coordinates mapped per defense coordinate name. - --- @field #AI_AIR_DISPATCHER.DefenseCoordinates DefenseCoordinates + -- @field #AI_AIR_DISPATCHER.DefenseCoordinates DefenseCoordinates AI_AIR_DISPATCHER.DefenseCoordinates = {} --- Enumerator for spawns at airbases -- @type AI_AIR_DISPATCHER.Takeoff -- @extends Wrapper.Group#GROUP.Takeoff - --- @field #AI_AIR_DISPATCHER.Takeoff Takeoff + -- @field #AI_AIR_DISPATCHER.Takeoff Takeoff AI_AIR_DISPATCHER.Takeoff = GROUP.Takeoff --- Defnes Landing location. @@ -938,7 +938,7 @@ do -- AI_AIR_DISPATCHER -- @type AI_AIR_DISPATCHER.DefenseQueue -- @list<#AI_AIR_DISPATCHER.DefenseQueueItem> DefenseQueueItem A list of all defenses being queued ... - --- @field #AI_AIR_DISPATCHER.DefenseQueue DefenseQueue + -- @field #AI_AIR_DISPATCHER.DefenseQueue DefenseQueue AI_AIR_DISPATCHER.DefenseQueue = {} --- Defense approach types @@ -1130,7 +1130,7 @@ do -- AI_AIR_DISPATCHER end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self function AI_AIR_DISPATCHER:onafterStart( From, Event, To ) self:GetParent( self ).onafterStart( self, From, Event, To ) @@ -1141,7 +1141,7 @@ do -- AI_AIR_DISPATCHER for Resource = 1, DefenderSquadron.ResourceCount or 0 do self:ResourcePark( DefenderSquadron ) end - self:I( "Parked resources for squadron " .. DefenderSquadron.Name ) + self:T( "Parked resources for squadron " .. DefenderSquadron.Name ) end end @@ -1194,7 +1194,7 @@ do -- AI_AIR_DISPATCHER end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self function AI_AIR_DISPATCHER:ResourcePark( DefenderSquadron ) local TemplateID = math.random( 1, #DefenderSquadron.Spawn ) local Spawn = DefenderSquadron.Spawn[ TemplateID ] -- Core.Spawn#SPAWN @@ -1211,31 +1211,31 @@ do -- AI_AIR_DISPATCHER end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_AIR_DISPATCHER:OnEventBaseCaptured( EventData ) local AirbaseName = EventData.PlaceName -- The name of the airbase that was captured. - self:I( "Captured " .. AirbaseName ) + self:T( "Captured " .. AirbaseName ) -- Now search for all squadrons located at the airbase, and sanitize them. for SquadronName, Squadron in pairs( self.DefenderSquadrons ) do if Squadron.AirbaseName == AirbaseName then Squadron.ResourceCount = -999 -- The base has been captured, and the resources are eliminated. No more spawning. Squadron.Captured = true - self:I( "Squadron " .. SquadronName .. " captured." ) + self:T( "Squadron " .. SquadronName .. " captured." ) end end end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_AIR_DISPATCHER:OnEventCrashOrDead( EventData ) self.Detection:ForgetDetectedUnit( EventData.IniUnitName ) end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_AIR_DISPATCHER:OnEventLand( EventData ) self:F( "Landed" ) @@ -1252,7 +1252,7 @@ do -- AI_AIR_DISPATCHER self:RemoveDefenderFromSquadron( Squadron, Defender ) end DefenderUnit:Destroy() - self:ResourcePark( Squadron, Defender ) + self:ResourcePark( Squadron ) return end if DefenderUnit:GetLife() ~= DefenderUnit:GetLife0() then @@ -1263,7 +1263,7 @@ do -- AI_AIR_DISPATCHER end end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_AIR_DISPATCHER:OnEventEngineShutdown( EventData ) local DefenderUnit = EventData.IniUnit @@ -1279,31 +1279,31 @@ do -- AI_AIR_DISPATCHER self:RemoveDefenderFromSquadron( Squadron, Defender ) end DefenderUnit:Destroy() - self:ResourcePark( Squadron, Defender ) + self:ResourcePark( Squadron ) end end end do -- Manage the defensive behaviour - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param #string DefenseCoordinateName The name of the coordinate to be defended by AIR defenses. -- @param Core.Point#COORDINATE DefenseCoordinate The coordinate to be defended by AIR defenses. function AI_AIR_DISPATCHER:AddDefenseCoordinate( DefenseCoordinateName, DefenseCoordinate ) self.DefenseCoordinates[DefenseCoordinateName] = DefenseCoordinate end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self function AI_AIR_DISPATCHER:SetDefenseReactivityLow() self.DefenseReactivity = 0.05 end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self function AI_AIR_DISPATCHER:SetDefenseReactivityMedium() self.DefenseReactivity = 0.15 end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self function AI_AIR_DISPATCHER:SetDefenseReactivityHigh() self.DefenseReactivity = 0.5 end @@ -1867,7 +1867,7 @@ do -- AI_AIR_DISPATCHER end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param #string SquadronName The squadron name. -- @param #number TakeoffInterval Only Takeoff new units each specified interval in seconds in 10 seconds steps. -- @usage @@ -2769,7 +2769,7 @@ do -- AI_AIR_DISPATCHER -- TODO: Need to model the resources in a squadron. - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param AI.AI_Air_Squadron#AI_AIR_SQUADRON Squadron function AI_AIR_DISPATCHER:AddDefenderToSquadron( Squadron, Defender, Size ) self.Defenders = self.Defenders or {} @@ -2782,7 +2782,7 @@ do -- AI_AIR_DISPATCHER self:F( { DefenderName = DefenderName, SquadronResourceCount = Squadron.ResourceCount } ) end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param AI.AI_Air_Squadron#AI_AIR_SQUADRON Squadron function AI_AIR_DISPATCHER:RemoveDefenderFromSquadron( Squadron, Defender ) self.Defenders = self.Defenders or {} @@ -2795,7 +2795,7 @@ do -- AI_AIR_DISPATCHER self:F( { DefenderName = DefenderName, SquadronResourceCount = SquadronResourceCount } ) end - --- @param #AI_AIR_DISPATCHER self + -- @param #AI_AIR_DISPATCHER self -- @param Wrapper.Group#GROUP Defender -- @return AI.AI_Air_Squadron#AI_AIR_SQUADRON The Squadron. function AI_AIR_DISPATCHER:GetSquadronFromDefender( Defender ) diff --git a/Moose Development/Moose/AI/AI_Air_Engage.lua b/Moose Development/Moose/AI/AI_Air_Engage.lua index db6a0a314..70898d2ba 100644 --- a/Moose Development/Moose/AI/AI_Air_Engage.lua +++ b/Moose Development/Moose/AI/AI_Air_Engage.lua @@ -13,8 +13,8 @@ ---- @type AI_AIR_ENGAGE --- @extends AI.AI_Air#AI_AIR +-- @type AI_AIR_ENGAGE +-- @extends AI.AI_AIR#AI_AIR --- Implements the core functions to intercept intruders. Use the Engage trigger to intercept intruders. @@ -351,7 +351,7 @@ function AI_AIR_ENGAGE:onafterAbort( AIGroup, From, Event, To ) end ---- @param #AI_AIR_ENGAGE self +-- @param #AI_AIR_ENGAGE self -- @param Wrapper.Group#GROUP AIGroup The Group Object managed by the FSM. -- @param #string From The From State string. -- @param #string Event The Event string. @@ -361,7 +361,7 @@ function AI_AIR_ENGAGE:onafterAccomplish( AIGroup, From, Event, To ) --self:SetDetectionOff() end ---- @param #AI_AIR_ENGAGE self +-- @param #AI_AIR_ENGAGE self -- @param Wrapper.Group#GROUP AIGroup The Group Object managed by the FSM. -- @param #string From The From State string. -- @param #string Event The Event string. @@ -374,7 +374,7 @@ function AI_AIR_ENGAGE:onafterDestroy( AIGroup, From, Event, To, EventData ) end end ---- @param #AI_AIR_ENGAGE self +-- @param #AI_AIR_ENGAGE self -- @param Core.Event#EVENTDATA EventData function AI_AIR_ENGAGE:OnEventDead( EventData ) self:F( { "EventDead", EventData } ) @@ -387,9 +387,9 @@ function AI_AIR_ENGAGE:OnEventDead( EventData ) end ---- @param Wrapper.Group#GROUP AIControllable +-- @param Wrapper.Group#GROUP AIControllable function AI_AIR_ENGAGE.___EngageRoute( AIGroup, Fsm, AttackSetUnit ) - Fsm:I(string.format("AI_AIR_ENGAGE.___EngageRoute: %s", tostring(AIGroup:GetName()))) + Fsm:T(string.format("AI_AIR_ENGAGE.___EngageRoute: %s", tostring(AIGroup:GetName()))) if AIGroup and AIGroup:IsAlive() then Fsm:__EngageRoute( Fsm.TaskDelay or 0.1, AttackSetUnit ) @@ -397,14 +397,14 @@ function AI_AIR_ENGAGE.___EngageRoute( AIGroup, Fsm, AttackSetUnit ) end ---- @param #AI_AIR_ENGAGE self +-- @param #AI_AIR_ENGAGE self -- @param Wrapper.Group#GROUP DefenderGroup The GroupGroup managed by the FSM. -- @param #string From The From State string. -- @param #string Event The Event string. -- @param #string To The To State string. -- @param Core.Set#SET_UNIT AttackSetUnit Unit set to be attacked. function AI_AIR_ENGAGE:onafterEngageRoute( DefenderGroup, From, Event, To, AttackSetUnit ) - self:I( { DefenderGroup, From, Event, To, AttackSetUnit } ) + self:T( { DefenderGroup, From, Event, To, AttackSetUnit } ) local DefenderGroupName = DefenderGroup:GetName() @@ -426,7 +426,13 @@ function AI_AIR_ENGAGE:onafterEngageRoute( DefenderGroup, From, Event, To, Attac local DefenderCoord = DefenderGroup:GetPointVec3() DefenderCoord:SetY( EngageAltitude ) -- Ground targets don't have an altitude. - local TargetCoord = AttackSetUnit:GetFirst():GetPointVec3() + local TargetCoord = AttackSetUnit:GetRandomSurely():GetPointVec3() + + if TargetCoord == nil then + self:Return() + return + end + TargetCoord:SetY( EngageAltitude ) -- Ground targets don't have an altitude. local TargetDistance = DefenderCoord:Get2DDistance( TargetCoord ) @@ -435,12 +441,12 @@ function AI_AIR_ENGAGE:onafterEngageRoute( DefenderGroup, From, Event, To, Attac -- TODO: A factor of * 3 is way too close. This causes the AI not to engange until merged sometimes! if TargetDistance <= EngageDistance * 9 then - --self:I(string.format("AI_AIR_ENGAGE onafterEngageRoute ==> __Engage - target distance = %.1f km", TargetDistance/1000)) + --self:T(string.format("AI_AIR_ENGAGE onafterEngageRoute ==> __Engage - target distance = %.1f km", TargetDistance/1000)) self:__Engage( 0.1, AttackSetUnit ) else - --self:I(string.format("FF AI_AIR_ENGAGE onafterEngageRoute ==> Routing - target distance = %.1f km", TargetDistance/1000)) + --self:T(string.format("FF AI_AIR_ENGAGE onafterEngageRoute ==> Routing - target distance = %.1f km", TargetDistance/1000)) local EngageRoute = {} local AttackTasks = {} @@ -472,16 +478,16 @@ function AI_AIR_ENGAGE:onafterEngageRoute( DefenderGroup, From, Event, To, Attac end else -- TODO: This will make an A2A Dispatcher CAP flight to return rather than going back to patrolling! - self:I( DefenderGroupName .. ": No targets found -> Going RTB") + self:T( DefenderGroupName .. ": No targets found -> Going RTB") self:Return() end end ---- @param Wrapper.Group#GROUP AIControllable +-- @param Wrapper.Group#GROUP AIControllable function AI_AIR_ENGAGE.___Engage( AIGroup, Fsm, AttackSetUnit ) - Fsm:I(string.format("AI_AIR_ENGAGE.___Engage: %s", tostring(AIGroup:GetName()))) + Fsm:T(string.format("AI_AIR_ENGAGE.___Engage: %s", tostring(AIGroup:GetName()))) if AIGroup and AIGroup:IsAlive() then local delay=Fsm.TaskDelay or 0.1 @@ -490,7 +496,7 @@ function AI_AIR_ENGAGE.___Engage( AIGroup, Fsm, AttackSetUnit ) end ---- @param #AI_AIR_ENGAGE self +-- @param #AI_AIR_ENGAGE self -- @param Wrapper.Group#GROUP DefenderGroup The GroupGroup managed by the FSM. -- @param #string From The From State string. -- @param #string Event The Event string. @@ -516,7 +522,7 @@ function AI_AIR_ENGAGE:onafterEngage( DefenderGroup, From, Event, To, AttackSetU local DefenderCoord = DefenderGroup:GetPointVec3() DefenderCoord:SetY( EngageAltitude ) -- Ground targets don't have an altitude. - local TargetCoord = AttackSetUnit:GetFirst():GetPointVec3() + local TargetCoord = AttackSetUnit:GetRandomSurely():GetPointVec3() if not TargetCoord then self:Return() return @@ -547,12 +553,12 @@ function AI_AIR_ENGAGE:onafterEngage( DefenderGroup, From, Event, To, AttackSetU local AttackUnitTasks = self:CreateAttackUnitTasks( AttackSetUnit, DefenderGroup, EngageAltitude ) -- Polymorphic if #AttackUnitTasks == 0 then - self:I( DefenderGroupName .. ": No valid targets found -> Going RTB") + self:T( DefenderGroupName .. ": No valid targets found -> Going RTB") self:Return() return else local text=string.format("%s: Engaging targets at distance %.2f NM", DefenderGroupName, UTILS.MetersToNM(TargetDistance)) - self:I(text) + self:T(text) DefenderGroup:OptionROEOpenFire() DefenderGroup:OptionROTEvadeFire() DefenderGroup:OptionKeepWeaponsOnThreat() @@ -569,13 +575,13 @@ function AI_AIR_ENGAGE:onafterEngage( DefenderGroup, From, Event, To, AttackSetU end else -- TODO: This will make an A2A Dispatcher CAP flight to return rather than going back to patrolling! - self:I( DefenderGroupName .. ": No targets found -> returning.") + self:T( DefenderGroupName .. ": No targets found -> returning.") self:Return() return end end ---- @param Wrapper.Group#GROUP AIEngage +-- @param Wrapper.Group#GROUP AIEngage function AI_AIR_ENGAGE.Resume( AIEngage, Fsm ) AIEngage:F( { "Resume:", AIEngage:GetName() } ) diff --git a/Moose Development/Moose/AI/AI_Air_Squadron.lua b/Moose Development/Moose/AI/AI_Air_Squadron.lua index 0c744b4ac..6651a92a5 100644 --- a/Moose Development/Moose/AI/AI_Air_Squadron.lua +++ b/Moose Development/Moose/AI/AI_Air_Squadron.lua @@ -13,7 +13,7 @@ ---- @type AI_AIR_SQUADRON +-- @type AI_AIR_SQUADRON -- @extends Core.Base#BASE @@ -38,7 +38,7 @@ AI_AIR_SQUADRON = { -- @return #AI_AIR_SQUADRON function AI_AIR_SQUADRON:New( SquadronName, AirbaseName, TemplatePrefixes, ResourceCount ) - self:I( { Air_Squadron = { SquadronName, AirbaseName, TemplatePrefixes, ResourceCount } } ) + self:T( { Air_Squadron = { SquadronName, AirbaseName, TemplatePrefixes, ResourceCount } } ) local AI_Air_Squadron = BASE:New() -- #AI_AIR_SQUADRON diff --git a/Moose Development/Moose/AI/AI_Cargo.lua b/Moose Development/Moose/AI/AI_Cargo.lua index 14a403c48..0bd6ab9ea 100644 --- a/Moose Development/Moose/AI/AI_Cargo.lua +++ b/Moose Development/Moose/AI/AI_Cargo.lua @@ -9,7 +9,7 @@ -- @module AI.AI_Cargo -- @image Cargo.JPG ---- @type AI_CARGO +-- @type AI_CARGO -- @extends Core.Fsm#FSM_CONTROLLABLE @@ -547,7 +547,7 @@ function AI_CARGO:onafterUnloaded( Carrier, From, Event, To, Cargo, CarrierUnit, for _, CarrierUnit in pairs( Carrier:GetUnits() ) do local CarrierUnit = CarrierUnit -- Wrapper.Unit#UNIT local IsEmpty = CarrierUnit:IsCargoEmpty() - self:I({ IsEmpty = IsEmpty }) + self:T({ IsEmpty = IsEmpty }) if not IsEmpty then AllUnloaded = false break diff --git a/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua b/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua index 0fedc9643..71b7f9f43 100644 --- a/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua +++ b/Moose Development/Moose/AI/AI_Cargo_Dispatcher.lua @@ -116,7 +116,7 @@ -- @image AI_Cargo_Dispatcher.JPG ---- @type AI_CARGO_DISPATCHER +-- @type AI_CARGO_DISPATCHER -- @field Core.Set#SET_GROUP CarrierSet The set of @{Wrapper.Group#GROUP} objects of carriers that will transport the cargo. -- @field Core.Set#SET_CARGO CargoSet The set of @{Cargo.Cargo#CARGO} objects, which can be CARGO_GROUP, CARGO_CRATE, CARGO_SLINGLOAD objects. -- @field Core.Zone#SET_ZONE PickupZoneSet The set of pickup zones, which are used to where the cargo can be picked up by the carriers. If nil, then cargo can be picked up everywhere. @@ -1161,7 +1161,7 @@ function AI_CARGO_DISPATCHER:onafterMonitor() else local text=string.format("WARNING: Cargo %s is too heavy to be loaded into transport. Cargo weight %.1f > %.1f load capacity of carrier %s.", tostring(Cargo:GetName()), Cargo:GetWeight(), LargestLoadCapacity, tostring(Carrier:GetName())) - self:I(text) + self:T(text) end end end diff --git a/Moose Development/Moose/AI/AI_Escort.lua b/Moose Development/Moose/AI/AI_Escort.lua index a063cc31d..ad325ed94 100644 --- a/Moose Development/Moose/AI/AI_Escort.lua +++ b/Moose Development/Moose/AI/AI_Escort.lua @@ -556,7 +556,7 @@ function AI_ESCORT:SetFlightMenuFormation( Formation ) if MenuFormation then local Arguments = MenuFormation.Arguments - --self:I({Arguments=unpack(Arguments)}) + --self:T({Arguments=unpack(Arguments)}) local FlightMenuFormation = MENU_GROUP:New( self.PlayerGroup, "Formation", self.MainMenu ) local MenuFlightFormationID = MENU_GROUP_COMMAND:New( self.PlayerGroup, Formation, FlightMenuFormation, function ( self, Formation, ... ) diff --git a/Moose Development/Moose/AI/AI_Escort_Dispatcher.lua b/Moose Development/Moose/AI/AI_Escort_Dispatcher.lua index 7bb869899..ff4c0ddfe 100644 --- a/Moose Development/Moose/AI/AI_Escort_Dispatcher.lua +++ b/Moose Development/Moose/AI/AI_Escort_Dispatcher.lua @@ -15,7 +15,7 @@ -- @image MOOSE.JPG ---- @type AI_ESCORT_DISPATCHER +-- @type AI_ESCORT_DISPATCHER -- @extends Core.Fsm#FSM @@ -33,7 +33,7 @@ AI_ESCORT_DISPATCHER = { ClassName = "AI_ESCORT_DISPATCHER", } ---- @field #list +-- @field #list AI_ESCORT_DISPATCHER.AI_Escorts = {} @@ -102,7 +102,7 @@ function AI_ESCORT_DISPATCHER:onafterStart( From, Event, To ) end ---- @param #AI_ESCORT_DISPATCHER self +-- @param #AI_ESCORT_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_ESCORT_DISPATCHER:OnEventExit( EventData ) @@ -110,11 +110,11 @@ function AI_ESCORT_DISPATCHER:OnEventExit( EventData ) local PlayerGroup = EventData.IniGroup local PlayerUnit = EventData.IniUnit - self:I({EscortAirbase= self.EscortAirbase } ) - self:I({PlayerGroupName = PlayerGroupName } ) - self:I({PlayerGroup = PlayerGroup}) - self:I({FirstGroup = self.CarrierSet:GetFirst()}) - self:I({FindGroup = self.CarrierSet:FindGroup( PlayerGroupName )}) + self:T({EscortAirbase= self.EscortAirbase } ) + self:T({PlayerGroupName = PlayerGroupName } ) + self:T({PlayerGroup = PlayerGroup}) + self:T({FirstGroup = self.CarrierSet:GetFirst()}) + self:T({FindGroup = self.CarrierSet:FindGroup( PlayerGroupName )}) if self.CarrierSet:FindGroup( PlayerGroupName ) then if self.AI_Escorts[PlayerGroupName] then @@ -125,7 +125,7 @@ function AI_ESCORT_DISPATCHER:OnEventExit( EventData ) end ---- @param #AI_ESCORT_DISPATCHER self +-- @param #AI_ESCORT_DISPATCHER self -- @param Core.Event#EVENTDATA EventData function AI_ESCORT_DISPATCHER:OnEventBirth( EventData ) @@ -133,17 +133,17 @@ function AI_ESCORT_DISPATCHER:OnEventBirth( EventData ) local PlayerGroup = EventData.IniGroup local PlayerUnit = EventData.IniUnit - self:I({EscortAirbase= self.EscortAirbase } ) - self:I({PlayerGroupName = PlayerGroupName } ) - self:I({PlayerGroup = PlayerGroup}) - self:I({FirstGroup = self.CarrierSet:GetFirst()}) - self:I({FindGroup = self.CarrierSet:FindGroup( PlayerGroupName )}) + self:T({EscortAirbase= self.EscortAirbase } ) + self:T({PlayerGroupName = PlayerGroupName } ) + self:T({PlayerGroup = PlayerGroup}) + self:T({FirstGroup = self.CarrierSet:GetFirst()}) + self:T({FindGroup = self.CarrierSet:FindGroup( PlayerGroupName )}) if self.CarrierSet:FindGroup( PlayerGroupName ) then if not self.AI_Escorts[PlayerGroupName] then local LeaderUnit = PlayerUnit local EscortGroup = self.EscortSpawn:SpawnAtAirbase( self.EscortAirbase, SPAWN.Takeoff.Hot ) - self:I({EscortGroup = EscortGroup}) + self:T({EscortGroup = EscortGroup}) self:ScheduleOnce( 1, function( EscortGroup ) diff --git a/Moose Development/Moose/AI/AI_Patrol.lua b/Moose Development/Moose/AI/AI_Patrol.lua index dd66f3fb7..d5ce61d72 100644 --- a/Moose Development/Moose/AI/AI_Patrol.lua +++ b/Moose Development/Moose/AI/AI_Patrol.lua @@ -652,15 +652,15 @@ function AI_PATROL_ZONE:onafterStart( Controllable, From, Event, To ) end ---- @param #AI_PATROL_ZONE self ---- @param Wrapper.Controllable#CONTROLLABLE Controllable +-- @param #AI_PATROL_ZONE self +-- @param Wrapper.Controllable#CONTROLLABLE Controllable+ function AI_PATROL_ZONE:onbeforeDetect( Controllable, From, Event, To ) return self.DetectOn and self.DetectActivated end ---- @param #AI_PATROL_ZONE self ---- @param Wrapper.Controllable#CONTROLLABLE Controllable +-- @param #AI_PATROL_ZONE self +-- @param Wrapper.Controllable#CONTROLLABLE Controllable function AI_PATROL_ZONE:onafterDetect( Controllable, From, Event, To ) local Detected = false @@ -705,7 +705,7 @@ function AI_PATROL_ZONE:onafterDetect( Controllable, From, Event, To ) end ---- @param Wrapper.Controllable#CONTROLLABLE AIControllable +-- @param Wrapper.Controllable#CONTROLLABLE AIControllable -- This static method is called from the route path within the last task at the last waypoint of the Controllable. -- Note that this method is required, as triggers the next route when patrolling for the Controllable. function AI_PATROL_ZONE:_NewPatrolRoute( AIControllable ) @@ -822,13 +822,13 @@ function AI_PATROL_ZONE:onafterRoute( Controllable, From, Event, To ) end ---- @param #AI_PATROL_ZONE self +-- @param #AI_PATROL_ZONE self function AI_PATROL_ZONE:onbeforeStatus() return self.CheckStatus end ---- @param #AI_PATROL_ZONE self +-- @param #AI_PATROL_ZONE self function AI_PATROL_ZONE:onafterStatus() self:F2() @@ -838,7 +838,7 @@ function AI_PATROL_ZONE:onafterStatus() local Fuel = self.Controllable:GetFuelMin() if Fuel < self.PatrolFuelThresholdPercentage then - self:I( self.Controllable:GetName() .. " is out of fuel:" .. Fuel .. ", RTB!" ) + self:T( self.Controllable:GetName() .. " is out of fuel:" .. Fuel .. ", RTB!" ) local OldAIControllable = self.Controllable local OrbitTask = OldAIControllable:TaskOrbitCircle( math.random( self.PatrolFloorAltitude, self.PatrolCeilingAltitude ), self.PatrolMinSpeed ) @@ -852,7 +852,7 @@ function AI_PATROL_ZONE:onafterStatus() -- TODO: Check GROUP damage function. local Damage = self.Controllable:GetLife() if Damage <= self.PatrolDamageThreshold then - self:I( self.Controllable:GetName() .. " is damaged:" .. Damage .. ", RTB!" ) + self:T( self.Controllable:GetName() .. " is damaged:" .. Damage .. ", RTB!" ) RTB = true end @@ -864,7 +864,7 @@ function AI_PATROL_ZONE:onafterStatus() end end ---- @param #AI_PATROL_ZONE self +-- @param #AI_PATROL_ZONE self function AI_PATROL_ZONE:onafterRTB() self:F2() @@ -903,13 +903,13 @@ function AI_PATROL_ZONE:onafterRTB() end ---- @param #AI_PATROL_ZONE self +-- @param #AI_PATROL_ZONE self function AI_PATROL_ZONE:onafterDead() self:SetDetectionOff() self:SetStatusOff() end ---- @param #AI_PATROL_ZONE self +-- @param #AI_PATROL_ZONE self -- @param Core.Event#EVENTDATA EventData function AI_PATROL_ZONE:OnCrash( EventData ) @@ -920,7 +920,7 @@ function AI_PATROL_ZONE:OnCrash( EventData ) end end ---- @param #AI_PATROL_ZONE self +-- @param #AI_PATROL_ZONE self -- @param Core.Event#EVENTDATA EventData function AI_PATROL_ZONE:OnEjection( EventData ) @@ -929,7 +929,7 @@ function AI_PATROL_ZONE:OnEjection( EventData ) end end ---- @param #AI_PATROL_ZONE self +-- @param #AI_PATROL_ZONE self -- @param Core.Event#EVENTDATA EventData function AI_PATROL_ZONE:OnPilotDead( EventData ) From 3b364c76506aa661d15c27e025b995a27bb2b0c3 Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Fri, 19 Apr 2024 15:57:21 +0200 Subject: [PATCH 07/15] #SPAWN - less noise --- Moose Development/Moose/Core/Spawn.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/Moose Development/Moose/Core/Spawn.lua b/Moose Development/Moose/Core/Spawn.lua index e9747fc9b..eb1c58efb 100644 --- a/Moose Development/Moose/Core/Spawn.lua +++ b/Moose Development/Moose/Core/Spawn.lua @@ -1778,8 +1778,6 @@ function SPAWN:SpawnWithIndex( SpawnIndex, NoBirth ) SpawnTemplate.hidden=true end - self:I(SpawnTemplate) - -- Set country, coalition and category. SpawnTemplate.CategoryID = self.SpawnInitCategory or SpawnTemplate.CategoryID SpawnTemplate.CountryID = self.SpawnInitCountry or SpawnTemplate.CountryID From 95baed1aac89db3fcbc674dbcd03229f76996dc3 Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Fri, 19 Apr 2024 15:57:43 +0200 Subject: [PATCH 08/15] #GROUP - make GetCoordinate a bit more robust --- Moose Development/Moose/Wrapper/Group.lua | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Moose Development/Moose/Wrapper/Group.lua b/Moose Development/Moose/Wrapper/Group.lua index a15005b3c..96ac5d87f 100644 --- a/Moose Development/Moose/Wrapper/Group.lua +++ b/Moose Development/Moose/Wrapper/Group.lua @@ -1187,13 +1187,12 @@ end -- @return Core.Point#COORDINATE The COORDINATE of the GROUP. function GROUP:GetCoordinate() - local Units = self:GetUnits() or {} for _,_unit in pairs(Units) do local FirstUnit = _unit -- Wrapper.Unit#UNIT - if FirstUnit then + if FirstUnit and FirstUnit:IsAlive() then local FirstUnitCoordinate = FirstUnit:GetCoordinate() @@ -1205,6 +1204,22 @@ function GROUP:GetCoordinate() end end + -- no luck, try the API way + + local DCSGroup = Group.getByName(self.GroupName) + local DCSUnits = DCSGroup:getUnits() or {} + for _,_unit in pairs(DCSUnits) do + if Object.isExist(_unit) then + local position = _unit:getPosition() + local point = position.p ~= nil and position.p or _unit:GetPoint() + if point then + --self:I(point) + local coord = COORDINATE:NewFromVec3(point) + return coord + end + end + end + BASE:E( { "Cannot GetCoordinate", Group = self, Alive = self:IsAlive() } ) end From 1346317ad9d9e8c02c0895e9097030352fa70e9a Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Sat, 20 Apr 2024 16:21:02 +0200 Subject: [PATCH 09/15] #STRATEGO - add functions to set weight, baseweight manually# --- .../Moose/Functional/Stratego.lua | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/Moose Development/Moose/Functional/Stratego.lua b/Moose Development/Moose/Functional/Stratego.lua index 2655500f8..97bf316f5 100644 --- a/Moose Development/Moose/Functional/Stratego.lua +++ b/Moose Development/Moose/Functional/Stratego.lua @@ -177,7 +177,7 @@ STRATEGO = { debug = false, drawzone = false, markzone = false, - version = "0.2.5", + version = "0.2.6", portweight = 3, POIweight = 1, maxrunways = 3, @@ -759,9 +759,39 @@ function STRATEGO:GetNextHighestWeightNodes(Weight, Coalition) return airbases[weight],weight end +--- [USER] Set the aggregated weight of a single node found by its name manually. +-- @param #STRATEGO self +-- @param #string Name The name to look for. +-- @param #number Weight The weight to be set. +-- @return #boolean success +function STRATEGO:SetNodeWeight(Name,Weight) + self:T(self.lid.."SetNodeWeight") + if Name and Weight and self.airbasetable[Name] then + self.airbasetable[Name].weight = Weight or 0 + return true + else + return false + end +end + +--- [USER] Set the base weight of a single node found by its name manually. +-- @param #STRATEGO self +-- @param #string Name The name to look for. +-- @param #number Weight The weight to be set. +-- @return #boolean success +function STRATEGO:SetNodeBaseWeight(Name,Weight) + self:T(self.lid.."SetNodeBaseWeight") + if Name and Weight and self.airbasetable[Name] then + self.airbasetable[Name].baseweight = Weight or 0 + return true + else + return false + end +end + --- [USER] Get the aggregated weight of a node by its name. -- @param #STRATEGO self --- @param #string Name. +-- @param #string Name The name to look for. -- @return #number Weight The weight or 0 if not found. function STRATEGO:GetNodeWeight(Name) self:T(self.lid.."GetNodeWeight") @@ -774,7 +804,7 @@ end --- [USER] Get the base weight of a node by its name. -- @param #STRATEGO self --- @param #string Name. +-- @param #string Name The name to look for. -- @return #number Weight The base weight or 0 if not found. function STRATEGO:GetNodeBaseWeight(Name) self:T(self.lid.."GetNodeBaseWeight") @@ -787,7 +817,7 @@ end --- [USER] Get the COALITION of a node by its name. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return #number Coalition The coalition. function STRATEGO:GetNodeCoalition(Name) self:T(self.lid.."GetNodeCoalition") @@ -800,7 +830,7 @@ end --- [USER] Get the TYPE of a node by its name. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return #string Type Type of the node, e.g. STRATEGO.Type.AIRBASE or nil if not found. function STRATEGO:GetNodeType(Name) self:T(self.lid.."GetNodeType") @@ -813,7 +843,7 @@ end --- [USER] Get the ZONE of a node by its name. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return Core.Zone#ZONE Zone The Zone of the node or nil if not found. function STRATEGO:GetNodeZone(Name) self:T(self.lid.."GetNodeZone") @@ -826,7 +856,7 @@ end --- [USER] Get the OPSZONE of a node by its name. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return Ops.OpsZone#OPSZONE OpsZone The OpsZone of the node or nil if not found. function STRATEGO:GetNodeOpsZone(Name) self:T(self.lid.."GetNodeOpsZone") @@ -839,7 +869,7 @@ end --- [USER] Get the COORDINATE of a node by its name. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return Core.Point#COORDINATE Coordinate The Coordinate of the node or nil if not found. function STRATEGO:GetNodeCoordinate(Name) self:T(self.lid.."GetNodeCoordinate") @@ -852,7 +882,7 @@ end --- [USER] Check if the TYPE of a node is AIRBASE. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return #boolean Outcome function STRATEGO:IsAirbase(Name) self:T(self.lid.."IsAirbase") @@ -865,7 +895,7 @@ end --- [USER] Check if the TYPE of a node is PORT. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return #boolean Outcome function STRATEGO:IsPort(Name) self:T(self.lid.."IsPort") @@ -878,7 +908,7 @@ end --- [USER] Check if the TYPE of a node is POI. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return #boolean Outcome function STRATEGO:IsPOI(Name) self:T(self.lid.."IsPOI") @@ -891,7 +921,7 @@ end --- [USER] Check if the TYPE of a node is FARP. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return #boolean Outcome function STRATEGO:IsFARP(Name) self:T(self.lid.."IsFARP") @@ -904,7 +934,7 @@ end --- [USER] Check if the TYPE of a node is SHIP. -- @param #STRATEGO self --- @param #string Name. +-- @param #string The name to look for. -- @return #boolean Outcome function STRATEGO:IsShip(Name) self:T(self.lid.."IsShip") From 26deaca16632a2e16a854339f32170f0594f717d Mon Sep 17 00:00:00 2001 From: Niels Vaes Date: Sun, 21 Apr 2024 10:08:06 +0200 Subject: [PATCH 10/15] Adding SHAPES (#2110) * Adding a new TerminalType (100)that seems to be introduced in the update that brought Muwaffaq Salti. The base has a couple of spots (like 04, 05, 06) that can only accommodate smaller type fixed wing aircraft, like the F-16, but not bigger types like the Warthog of the Strike Eagle. Because we weren't checking for this new type, spawning in these particular spots always resulted in an airstart, because `_CheckTerminalType` would always return `false` * Adding Shapes over from old MOOSE branch * cleanup * adding HEXtoRGBA --- Moose Development/Moose/Modules.lua | 9 + Moose Development/Moose/Shapes/Circle.lua | 259 +++++++++++ Moose Development/Moose/Shapes/Cube.lua | 66 +++ Moose Development/Moose/Shapes/Line.lua | 331 ++++++++++++++ Moose Development/Moose/Shapes/Oval.lua | 213 +++++++++ Moose Development/Moose/Shapes/Polygon.lua | 458 +++++++++++++++++++ Moose Development/Moose/Shapes/ShapeBase.lua | 216 +++++++++ Moose Development/Moose/Shapes/Triangle.lua | 86 ++++ Moose Development/Moose/Utilities/Utils.lua | 56 ++- 9 files changed, 1685 insertions(+), 9 deletions(-) create mode 100644 Moose Development/Moose/Shapes/Circle.lua create mode 100644 Moose Development/Moose/Shapes/Cube.lua create mode 100644 Moose Development/Moose/Shapes/Line.lua create mode 100644 Moose Development/Moose/Shapes/Oval.lua create mode 100644 Moose Development/Moose/Shapes/Polygon.lua create mode 100644 Moose Development/Moose/Shapes/ShapeBase.lua create mode 100644 Moose Development/Moose/Shapes/Triangle.lua diff --git a/Moose Development/Moose/Modules.lua b/Moose Development/Moose/Modules.lua index c483b8d21..c95366e45 100644 --- a/Moose Development/Moose/Modules.lua +++ b/Moose Development/Moose/Modules.lua @@ -122,6 +122,15 @@ __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Route.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Account.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Assist.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/ShapeBase.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Circle.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Cube.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Line.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Oval.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Polygon.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Triangle.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Arrow.lua' ) + __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/UserSound.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/SoundOutput.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/Radio.lua' ) diff --git a/Moose Development/Moose/Shapes/Circle.lua b/Moose Development/Moose/Shapes/Circle.lua new file mode 100644 index 000000000..04c153d86 --- /dev/null +++ b/Moose Development/Moose/Shapes/Circle.lua @@ -0,0 +1,259 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.CIRCLE + +--- CIRCLE class. +-- @type CIRCLE +-- @field #string ClassName Name of the class. +-- @field #number Radius Radius of the circle + +--- *It's NOT hip to be square* -- Someone, somewhere, probably +-- +-- === +-- +-- # CIRCLE +-- CIRCLEs can be fetched from the drawings in the Mission Editor + +-- This class has some of the standard CIRCLE functions you'd expect. One function of interest is CIRCLE:PointInSector() that you can use if a point is +-- within a certain sector (pizza slice) of a circle. This can be useful for many things, including rudimentary, "radar-like" searches from a unit. + +-- @field #CIRCLE + +--- CIRCLE class with properties and methods for handling circles. +CIRCLE = { + ClassName = "CIRCLE", + Radius = nil, +} +--- Finds a circle on the map by its name. The circle must have been added in the Mission Editor +-- @param #string shape_name Name of the circle to find +-- @return #CIRCLE The found circle, or nil if not found +function CIRCLE:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.find(object["name"], shape_name, 1, true) then + if object["polygonMode"] == "circle" then + self.Radius = object["radius"] + end + end + end + end + + return self +end + +--- Finds a circle by its name in the database. +-- @param #string shape_name Name of the circle to find +-- @return #CIRCLE The found circle, or nil if not found +function CIRCLE:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new circle from a center point and a radius. +-- @param #table vec2 The center point of the circle +-- @param #number radius The radius of the circle +-- @return #CIRCLE The new circle +function CIRCLE:New(vec2, radius) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = vec2 + self.Radius = radius + return self +end + +--- Gets the radius of the circle. +-- @return #number The radius of the circle +function CIRCLE:GetRadius() + return self.Radius +end + +--- Checks if a point is contained within the circle. +-- @param #table point The point to check +-- @return #bool True if the point is contained, false otherwise +function CIRCLE:ContainsPoint(point) + if ((point.x - self.CenterVec2.x) ^ 2 + (point.y - self.CenterVec2.y) ^ 2) ^ 0.5 <= self.Radius then + return true + end + return false +end + +--- Checks if a point is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #table point The point to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if the point is contained, false otherwise +function CIRCLE:PointInSector(point, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + local function are_clockwise(v1, v2) + return -v1.x * v2.y + v1.y * v2.x > 0 + end + + local function is_in_radius(rp) + return rp.x * rp.x + rp.y * rp.y <= radius ^ 2 + end + + local rel_pt = { + x = point.x - center.x, + y = point.y - center.y + } + + local rel_sector_start = { + x = sector_start.x - center.x, + y = sector_start.y - center.y, + } + + local rel_sector_end = { + x = sector_end.x - center.x, + y = sector_end.y - center.y, + } + + return not are_clockwise(rel_sector_start, rel_pt) and + are_clockwise(rel_sector_end, rel_pt) and + is_in_radius(rel_pt, radius) +end + +--- Checks if a unit is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string unit_name The name of the unit to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if the unit is contained, false otherwise +function CIRCLE:UnitInSector(unit_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + if self:PointInSector(UNIT:FindByName(unit_name):GetVec2(), sector_start, sector_end, center, radius) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string group_name The name of the group to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if any unit of the group is contained, false otherwise +function CIRCLE:AnyOfGroupInSector(group_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string group_name The name of the group to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if all units of the group are contained, false otherwise +function CIRCLE:AllOfGroupInSector(group_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if not self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then + return false + end + end + return true +end + +--- Checks if a unit is contained within a radius of the circle. +-- @param #string unit_name The name of the unit to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if the unit is contained, false otherwise +function CIRCLE:UnitInRadius(unit_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + if UTILS.IsInRadius(center, UNIT:FindByName(unit_name):GetVec2(), radius) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within a radius of the circle. +-- @param #string group_name The name of the group to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if any unit of the group is contained, false otherwise +function CIRCLE:AnyOfGroupInRadius(group_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if UTILS.IsInRadius(center, unit:GetVec2(), radius) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within a radius of the circle. +-- @param #string group_name The name of the group to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if all units of the group are contained, false otherwise +function CIRCLE:AllOfGroupInRadius(group_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if not UTILS.IsInRadius(center, unit:GetVec2(), radius) then + return false + end + end + return true +end + +--- Returns a random Vec2 within the circle. +-- @return #table The random Vec2 +function CIRCLE:GetRandomVec2() + local angle = math.random() * 2 * math.pi + + local rx = math.random(0, self.Radius) * math.cos(angle) + self.CenterVec2.x + local ry = math.random(0, self.Radius) * math.sin(angle) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Returns a random Vec2 on the border of the circle. +-- @return #table The random Vec2 +function CIRCLE:GetRandomVec2OnBorder() + local angle = math.random() * 2 * math.pi + + local rx = self.Radius * math.cos(angle) + self.CenterVec2.x + local ry = self.Radius * math.sin(angle) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Calculates the bounding box of the circle. The bounding box is the smallest rectangle that contains the circle. +-- @return #table The bounding box of the circle +function CIRCLE:GetBoundingBox() + local min_x = self.CenterVec2.x - self.Radius + local min_y = self.CenterVec2.y - self.Radius + local max_x = self.CenterVec2.x + self.Radius + local max_y = self.CenterVec2.y + self.Radius + + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + diff --git a/Moose Development/Moose/Shapes/Cube.lua b/Moose Development/Moose/Shapes/Cube.lua new file mode 100644 index 000000000..ae3f73090 --- /dev/null +++ b/Moose Development/Moose/Shapes/Cube.lua @@ -0,0 +1,66 @@ +CUBE = { + ClassName = "CUBE", + Points = {}, + Coords = {} +} + +--- Points need to be added in the following order: +--- p1 -> p4 make up the front face of the cube +--- p5 -> p8 make up the back face of the cube +--- p1 connects to p5 +--- p2 connects to p6 +--- p3 connects to p7 +--- p4 connects to p8 +--- +--- 8-----------7 +--- /| /| +--- / | / | +--- 4--+--------3 | +--- | | | | +--- | | | | +--- | | | | +--- | 5--------+--6 +--- | / | / +--- |/ |/ +--- 1-----------2 +--- +function CUBE:New(p1, p2, p3, p4, p5, p6, p7, p8) + local self = BASE:Inherit(self, SHAPE_BASE) + self.Points = {p1, p2, p3, p4, p5, p6, p7, p8} + for _, point in spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec3(point)) + end + return self +end + +function CUBE:GetCenter() + local center = { x=0, y=0, z=0 } + for _, point in pairs(self.Points) do + center.x = center.x + point.x + center.y = center.y + point.y + center.z = center.z + point.z + end + + center.x = center.x / 8 + center.y = center.y / 8 + center.z = center.z / 8 + return center +end + +function CUBE:ContainsPoint(point, cube_points) + cube_points = cube_points or self.Points + local min_x, min_y, min_z = math.huge, math.huge, math.huge + local max_x, max_y, max_z = -math.huge, -math.huge, -math.huge + + -- Find the minimum and maximum x, y, and z values of the cube points + for _, p in ipairs(cube_points) do + if p.x < min_x then min_x = p.x end + if p.y < min_y then min_y = p.y end + if p.z < min_z then min_z = p.z end + if p.x > max_x then max_x = p.x end + if p.y > max_y then max_y = p.y end + if p.z > max_z then max_z = p.z end + end + + return point.x >= min_x and point.x <= max_x and point.y >= min_y and point.y <= max_y and point.z >= min_z and point.z <= max_z +end diff --git a/Moose Development/Moose/Shapes/Line.lua b/Moose Development/Moose/Shapes/Line.lua new file mode 100644 index 000000000..08f7c84a0 --- /dev/null +++ b/Moose Development/Moose/Shapes/Line.lua @@ -0,0 +1,331 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.LINE + +--- OVAL class. +-- @type OVAL +-- @field #string ClassName Name of the class. +-- @field #number Points points of the line +-- @field #number Coords coordinates of the line + +-- +-- === + +-- @field #LINE +LINE = { + ClassName = "LINE", + Points = {}, + Coords = {}, +} + +--- Finds a line on the map by its name. The line must be drawn in the Mission Editor +-- @param #string line_name Name of the line to find +-- @return #LINE The found line, or nil if not found +function LINE:FindOnMap(line_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(line_name)) + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == line_name then + if object["primitiveType"] == "Line" then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + table.insert(self.Points, p) + table.insert(self.Coords, coord) + end + end + end + end + end + + self:I(#self.Points) + if #self.Points == 0 then + return nil + end + + self.MarkIDs = {} + + return self +end + +--- Finds a line by its name in the database. +-- @param #string shape_name Name of the line to find +-- @return #LINE The found line, or nil if not found +function LINE:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new line from two points. +-- @param #table vec2 The first point of the line +-- @param #number radius The second point of the line +-- @return #LINE The new line +function LINE:New(...) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.Points = {...} + self:I(self.Points) + for _, point in UTILS.spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + return self +end + +--- Creates a new line from a circle. +-- @param #table center_point center point of the circle +-- @param #number radius radius of the circle, half length of the line +-- @param #number angle_degrees degrees the line will form from center point +-- @return #LINE The new line +function LINE:NewFromCircle(center_point, radius, angle_degrees) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = center_point + local angleRadians = math.rad(angle_degrees) + + local point1 = { + x = center_point.x + radius * math.cos(angleRadians), + y = center_point.y + radius * math.sin(angleRadians) + } + + local point2 = { + x = center_point.x + radius * math.cos(angleRadians + math.pi), + y = center_point.y + radius * math.sin(angleRadians + math.pi) + } + + for _, point in pairs{point1, point2} do + table.insert(self.Points, point) + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + + return self +end + +--- Gets the coordinates of the line. +-- @return #table The coordinates of the line +function LINE:Coordinates() + return self.Coords +end + +--- Gets the start coordinate of the line. The start coordinate is the first point of the line. +-- @return #COORDINATE The start coordinate of the line +function LINE:GetStartCoordinate() + return self.Coords[1] +end + +--- Gets the end coordinate of the line. The end coordinate is the last point of the line. +-- @return #COORDINATE The end coordinate of the line +function LINE:GetEndCoordinate() + return self.Coords[#self.Coords] +end + +--- Gets the start point of the line. The start point is the first point of the line. +-- @return #table The start point of the line +function LINE:GetStartPoint() + return self.Points[1] +end + +--- Gets the end point of the line. The end point is the last point of the line. +-- @return #table The end point of the line +function LINE:GetEndPoint() + return self.Points[#self.Points] +end + +--- Gets the length of the line. +-- @return #number The length of the line +function LINE:GetLength() + local total_length = 0 + for i=1, #self.Points - 1 do + local x1, y1 = self.Points[i]["x"], self.Points[i]["y"] + local x2, y2 = self.Points[i+1]["x"], self.Points[i+1]["y"] + local segment_length = math.sqrt((x2 - x1)^2 + (y2 - y1)^2) + total_length = total_length + segment_length + end + return total_length +end + +--- Returns a random point on the line. +-- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it +-- @return #table The random point +function LINE:GetRandomPoint(points) + points = points or self.Points + local rand = math.random() -- 0->1 + + local random_x = points[1].x + rand * (points[2].x - points[1].x) + local random_y = points[1].y + rand * (points[2].y - points[1].y) + + return { x= random_x, y= random_y } +end + +--- Gets the heading of the line. +-- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it +-- @return #number The heading of the line +function LINE:GetHeading(points) + points = points or self.Points + + local angle = math.atan2(points[2].y - points[1].y, points[2].x - points[1].x) + + angle = math.deg(angle) + if angle < 0 then + angle = angle + 360 + end + + return angle +end + + +--- Return each part of the line as a new line +-- @return #table The points +function LINE:GetIndividualParts() + local parts = {} + if #self.Points == 2 then + parts = {self} + end + + for i=1, #self.Points -1 do + local p1 = self.Points[i] + local p2 = self.Points[i % #self.Points + 1] + table.add(parts, LINE:New(p1, p2)) + end + + return parts +end + +--- Gets a number of points in between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @return #table The points +function LINE:GetPointsInbetween(amount, start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + if amount == 0 then return {start_point, end_point} end + + amount = amount + 1 + local points = {} + + local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } + local divided = { x = difference.x / amount, y = difference.y / amount } + + for j=0, amount do + local part_pos = {x = divided.x * j, y = divided.y * j} + -- add part_pos vector to the start point so the new point is placed along in the line + local point = {x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} + table.insert(points, point) + end + return points +end + +--- Gets a number of points in between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @return #table The points +function LINE:GetCoordinatesInBetween(amount, start_point, end_point) + local coords = {} + for _, pt in pairs(self:GetPointsInbetween(amount, start_point, end_point)) do + table.add(coords, COORDINATE:NewFromVec2(pt)) + end + return coords +end + + +function LINE:GetRandomPoint(start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + + local fraction = math.random() + + local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } + local part_pos = {x = difference.x * fraction, y = difference.y * fraction} + local random_point = { x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} + + return random_point +end + + +function LINE:GetRandomCoordinate(start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + + return COORDINATE:NewFromVec2(self:GetRandomPoint(start_point, end_point)) +end + + +--- Gets a number of points on a sine wave between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @param #number frequency (Optional) The frequency of the sine wave, default 1 +-- @param #number phase (Optional) The phase of the sine wave, default 0 +-- @param #number amplitude (Optional) The amplitude of the sine wave, default 100 +-- @return #table The points +function LINE:GetPointsBetweenAsSineWave(amount, start_point, end_point, frequency, phase, amplitude) + amount = amount or 20 + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + frequency = frequency or 1 -- number of cycles per unit of x + phase = phase or 0 -- offset in radians + amplitude = amplitude or 100 -- maximum height of the wave + + local points = {} + + -- Returns the y-coordinate of the sine wave at x + local function sine_wave(x) + return amplitude * math.sin(2 * math.pi * frequency * (x - start_point.x) + phase) + end + + -- Plot x-amount of points on the sine wave between point_01 and point_02 + local x = start_point.x + local step = (end_point.x - start_point.x) / 20 + for _=1, amount do + local y = sine_wave(x) + x = x + step + table.add(points, {x=x, y=y}) + end + return points +end + +--- Calculates the bounding box of the line. The bounding box is the smallest rectangle that contains the line. +-- @return #table The bounding box of the line +function LINE:GetBoundingBox() + local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[2].x, self.Points[2].y + + for i = 2, #self.Points do + local x, y = self.Points[i].x, self.Points[i].y + + if x < min_x then + min_x = x + end + if y < min_y then + min_y = y + end + if x > max_x then + max_x = x + end + if y > max_y then + max_y = y + end + end + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Draws the line on the map. +-- @param #table points The points of the line +function LINE:Draw() + for i=1, #self.Coords -1 do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.MarkIDs, c1:LineToAll(c2)) + end +end + +--- Removes the drawing of the line from the map. +function LINE:RemoveDraw() + for _, mark_id in pairs(self.MarkIDs) do + UTILS.RemoveMark(mark_id) + end +end diff --git a/Moose Development/Moose/Shapes/Oval.lua b/Moose Development/Moose/Shapes/Oval.lua new file mode 100644 index 000000000..d2f85a822 --- /dev/null +++ b/Moose Development/Moose/Shapes/Oval.lua @@ -0,0 +1,213 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.OVAL + +--- OVAL class. +-- @type OVAL +-- @field #string ClassName Name of the class. +-- @field #number MajorAxis The major axis (radius) of the oval +-- @field #number MinorAxis The minor axis (radius) of the oval +-- @field #number Angle The angle the oval is rotated on + +--- *The little man removed his hat, what an egg shaped head he had* -- Agatha Christie +-- +-- === +-- +-- # OVAL +-- OVALs can be fetched from the drawings in the Mission Editor + +-- The major and minor axes define how elongated the shape of an oval is. This class has some basic functions that the other SHAPE classes have as well. +-- Since it's not possible to draw the shape of an oval while the mission is running, right now the draw function draws 2 cicles. One with the major axis and one with +-- the minor axis. It then draws a diamond shape on an angle where the corners touch the major and minor axes to give an indication of what the oval actually +-- looks like. + +-- Using ovals can be handy to find an area on the ground that is actually an intersection of a cone and a plane. So imagine you're faking the view cone of +-- a targeting pod and + +-- @field #OVAL + +--- OVAL class with properties and methods for handling ovals. +OVAL = { + ClassName = "OVAL", + MajorAxis = nil, + MinorAxis = nil, + Angle = 0, + DrawPoly=nil +} + +--- Finds an oval on the map by its name. The oval must be drawn on the map. +-- @param #string shape_name Name of the oval to find +-- @return #OVAL The found oval, or nil if not found +function OVAL:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.find(object["name"], shape_name, 1, true) then + if object["polygonMode"] == "oval" then + self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } + self.MajorAxis = object["r1"] + self.MinorAxis = object["r2"] + self.Angle = object["angle"] + end + end + end + end + + return self +end + +--- Finds an oval by its name in the database. +-- @param #string shape_name Name of the oval to find +-- @return #OVAL The found oval, or nil if not found +function OVAL:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new oval from a center point, major axis, minor axis, and angle. +-- @param #table vec2 The center point of the oval +-- @param #number major_axis The major axis of the oval +-- @param #number minor_axis The minor axis of the oval +-- @param #number angle The angle of the oval +-- @return #OVAL The new oval +function OVAL:New(vec2, major_axis, minor_axis, angle) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = vec2 + self.MajorAxis = major_axis + self.MinorAxis = minor_axis + self.Angle = angle or 0 + + return self +end + +--- Gets the major axis of the oval. +-- @return #number The major axis of the oval +function OVAL:GetMajorAxis() + return self.MajorAxis +end + +--- Gets the minor axis of the oval. +-- @return #number The minor axis of the oval +function OVAL:GetMinorAxis() + return self.MinorAxis +end + +--- Gets the angle of the oval. +-- @return #number The angle of the oval +function OVAL:GetAngle() + return self.Angle +end + +--- Sets the major axis of the oval. +-- @param #number value The new major axis +function OVAL:SetMajorAxis(value) + self.MajorAxis = value +end + +--- Sets the minor axis of the oval. +-- @param #number value The new minor axis +function OVAL:SetMinorAxis(value) + self.MinorAxis = value +end + +--- Sets the angle of the oval. +-- @param #number value The new angle +function OVAL:SetAngle(value) + self.Angle = value +end + +--- Checks if a point is contained within the oval. +-- @param #table point The point to check +-- @return #bool True if the point is contained, false otherwise +function OVAL:ContainsPoint(point) + local cos, sin = math.cos, math.sin + local dx = point.x - self.CenterVec2.x + local dy = point.y - self.CenterVec2.y + local rx = dx * cos(self.Angle) + dy * sin(self.Angle) + local ry = -dx * sin(self.Angle) + dy * cos(self.Angle) + return rx * rx / (self.MajorAxis * self.MajorAxis) + ry * ry / (self.MinorAxis * self.MinorAxis) <= 1 +end + +--- Returns a random Vec2 within the oval. +-- @return #table The random Vec2 +function OVAL:GetRandomVec2() + local theta = math.rad(self.Angle) + + local random_point = math.sqrt(math.random()) --> uniformly + --local random_point = math.random() --> more clumped around center + local phi = math.random() * 2 * math.pi + local x_c = random_point * math.cos(phi) + local y_c = random_point * math.sin(phi) + local x_e = x_c * self.MajorAxis + local y_e = y_c * self.MinorAxis + local rx = (x_e * math.cos(theta) - y_e * math.sin(theta)) + self.CenterVec2.x + local ry = (x_e * math.sin(theta) + y_e * math.cos(theta)) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Calculates the bounding box of the oval. The bounding box is the smallest rectangle that contains the oval. +-- @return #table The bounding box of the oval +function OVAL:GetBoundingBox() + local min_x = self.CenterVec2.x - self.MajorAxis + local min_y = self.CenterVec2.y - self.MinorAxis + local max_x = self.CenterVec2.x + self.MajorAxis + local max_y = self.CenterVec2.y + self.MinorAxis + + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Draws the oval on the map, for debugging +-- @param #number angle (Optional) The angle of the oval. If nil will use self.Angle +function OVAL:Draw() + --for pt in pairs(self:PointsOnEdge(20)) do + -- COORDINATE:NewFromVec2(pt) + --end + + self.DrawPoly = POLYGON:NewFromPoints(self:PointsOnEdge(20)) + self.DrawPoly:Draw(true) + + + + + ---- TODO: draw a better shape using line segments + --angle = angle or self.Angle + --local coor = self:GetCenterCoordinate() + -- + --table.add(self.MarkIDs, coor:CircleToAll(self.MajorAxis)) + --table.add(self.MarkIDs, coor:CircleToAll(self.MinorAxis)) + --table.add(self.MarkIDs, coor:LineToAll(coor:Translate(self.MajorAxis, self.Angle))) + -- + --local pt_1 = coor:Translate(self.MajorAxis, self.Angle) + --local pt_2 = coor:Translate(self.MinorAxis, self.Angle - 90) + --local pt_3 = coor:Translate(self.MajorAxis, self.Angle - 180) + --local pt_4 = coor:Translate(self.MinorAxis, self.Angle - 270) + --table.add(self.MarkIDs, pt_1:QuadToAll(pt_2, pt_3, pt_4), -1, {0, 1, 0}, 1, {0, 1, 0}) +end + +--- Removes the drawing of the oval from the map +function OVAL:RemoveDraw() + self.DrawPoly:RemoveDraw() +end + + +function OVAL:PointsOnEdge(num_points) + num_points = num_points or 20 + local points = {} + local dtheta = 2 * math.pi / num_points + + for i = 0, num_points - 1 do + local theta = i * dtheta + local x = self.CenterVec2.x + self.MajorAxis * math.cos(theta) * math.cos(self.Angle) - self.MinorAxis * math.sin(theta) * math.sin(self.Angle) + local y = self.CenterVec2.y + self.MajorAxis * math.cos(theta) * math.sin(self.Angle) + self.MinorAxis * math.sin(theta) * math.cos(self.Angle) + table.insert(points, {x = x, y = y}) + end + + return points +end + + diff --git a/Moose Development/Moose/Shapes/Polygon.lua b/Moose Development/Moose/Shapes/Polygon.lua new file mode 100644 index 000000000..a40256ecf --- /dev/null +++ b/Moose Development/Moose/Shapes/Polygon.lua @@ -0,0 +1,458 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.POLYGON + +--- POLYGON class. +-- @type POLYGON +-- @field #string ClassName Name of the class. +-- @field #table Points List of 3D points defining the shape, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table Triangles List of TRIANGLEs that make up the shape of the POLYGON after being triangulated +-- @extends Core.Base#BASE + +--- *Polygons are fashionable at the moment* -- Trip Hawkins +-- +-- === +-- +-- # POLYGON +-- POLYGONs can be fetched from the drawings in the Mission Editor if the drawing is: +-- * A closed shape made with line segments +-- * A closed shape made with a freehand line +-- * A freehand drawn polygon +-- * A rect +-- Use the POLYGON:FindOnMap() of POLYGON:Find() functions for this. You can also create a non existing polygon in memory using the POLYGON:New() function. Pass in a +-- any number of Vec2s into this function to define the shape of the polygon you want. + +-- You can draw very intricate and complex polygons in the Mission Editor to avoid (or include) map objects. You can then generate random points within this complex +-- shape for spawning groups or checking positions. + +-- When a POLYGON is made, it's automatically triangulated. The resulting triangles are stored in POLYGON.Triangles. This also immeadiately saves the surface area +-- of the POLYGON. Because the POLYGON is triangulated, it's possible to generate random points within this POLYGON without having to use a trial and error method to see if +-- the point is contained within the shape. +-- Using POLYGON:GetRandomVec2() will result in a truly, non-biased, random Vec2 within the shape. You'll want to use this function most. There's also POLYGON:GetRandomNonWeightedVec2 +-- which ignores the size of the triangles in the polygon to pick a random points. This will result in more points clumping together in parts of the polygon where the triangles are +-- the smallest. + + +-- @field #POLYGON + +POLYGON = { + ClassName = "POLYGON", + Points = {}, + Coords = {}, + Triangles = {}, + SurfaceArea = 0, + TriangleMarkIDs = {}, + OutlineMarkIDs = {}, + Angle = nil, -- for arrows + Heading = nil -- for arrows +} + +--- Finds a polygon on the map by its name. The polygon must be added in the mission editor. +-- @param #string shape_name Name of the polygon to find +-- @return #POLYGON The found polygon, or nil if not found +function POLYGON:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == shape_name then + if (object["primitiveType"] == "Line" and object["closed"] == true) or (object["polygonMode"] == "free") then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + self.Points[#self.Points + 1] = p + self.Coords[#self.Coords + 1] = coord + end + elseif object["polygonMode"] == "rect" then + local angle = object["angle"] + local half_width = object["width"] / 2 + local half_height = object["height"] / 2 + + local p1 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) + local p2 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) + local p3 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) + local p4 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) + + self.Points = {p1, p2, p3, p4} + for _, point in pairs(self.Points) do + self.Coords[#self.Coords + 1] = COORDINATE:NewFromVec2(point) + end + elseif object["polygonMode"] == "arrow" then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + self.Points[#self.Points + 1] = p + self.Coords[#self.Coords + 1] = coord + end + self.Angle = object["angle"] + self.Heading = UTILS.ClampAngle(self.Angle + 90) + end + end + end + end + + if #self.Points == 0 then + return nil + end + + self.CenterVec2 = self:GetCentroid() + self.Triangles = self:Triangulate() + self.SurfaceArea = self:__CalculateSurfaceArea() + + self.TriangleMarkIDs = {} + self.OutlineMarkIDs = {} + return self +end + +--- Creates a polygon from a zone. The zone must be defined in the mission. +-- @param #string zone_name Name of the zone +-- @return #POLYGON The polygon created from the zone, or nil if the zone is not found +function POLYGON:FromZone(zone_name) + for _, zone in pairs(env.mission.triggers.zones) do + if zone["name"] == zone_name then + return POLYGON:New(unpack(zone["verticies"] or {})) + end + end +end + +--- Finds a polygon by its name in the database. +-- @param #string shape_name Name of the polygon to find +-- @return #POLYGON The found polygon, or nil if not found +function POLYGON:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new polygon from a list of points. Each point is a table with 'x' and 'y' fields. +-- @param #table ... Points of the polygon +-- @return #POLYGON The new polygon +function POLYGON:New(...) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + + self.Points = {...} + self.Coords = {} + for _, point in UTILS.spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + self.Triangles = self:Triangulate() + self.SurfaceArea = self:__CalculateSurfaceArea() + + return self +end + +--- Calculates the centroid of the polygon. The centroid is the average of the 'x' and 'y' coordinates of the points. +-- @return #table The centroid of the polygon +function POLYGON:GetCentroid() + local function sum(t) + local total = 0 + for _, value in pairs(t) do + total = total + value + end + return total + end + + local x_values = {} + local y_values = {} + local length = table.length(self.Points) + + for _, point in pairs(self.Points) do + table.insert(x_values, point.x) + table.insert(y_values, point.y) + end + + local x = sum(x_values) / length + local y = sum(y_values) / length + + return { + ["x"] = x, + ["y"] = y + } +end + +--- Returns the coordinates of the polygon. Each coordinate is a COORDINATE object. +-- @return #table The coordinates of the polygon +function POLYGON:GetCoordinates() + return self.Coords +end + +--- Returns the start coordinate of the polygon. The start coordinate is the first point of the polygon. +-- @return #COORDINATE The start coordinate of the polygon +function POLYGON:GetStartCoordinate() + return self.Coords[1] +end + +--- Returns the end coordinate of the polygon. The end coordinate is the last point of the polygon. +-- @return #COORDINATE The end coordinate of the polygon +function POLYGON:GetEndCoordinate() + return self.Coords[#self.Coords] +end + +--- Returns the start point of the polygon. The start point is the first point of the polygon. +-- @return #table The start point of the polygon +function POLYGON:GetStartPoint() + return self.Points[1] +end + +--- Returns the end point of the polygon. The end point is the last point of the polygon. +-- @return #table The end point of the polygon +function POLYGON:GetEndPoint() + return self.Points[#self.Points] +end + +--- Returns the points of the polygon. Each point is a table with 'x' and 'y' fields. +-- @return #table The points of the polygon +function POLYGON:GetPoints() + return self.Points +end + +--- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. +-- @return #number The surface area of the polygon +function POLYGON:GetSurfaceArea() + return self.SurfaceArea +end + +--- Calculates the bounding box of the polygon. The bounding box is the smallest rectangle that contains the polygon. +-- @return #table The bounding box of the polygon +function POLYGON:GetBoundingBox() + local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[1].x, self.Points[1].y + + for i = 2, #self.Points do + local x, y = self.Points[i].x, self.Points[i].y + + if x < min_x then + min_x = x + end + if y < min_y then + min_y = y + end + if x > max_x then + max_x = x + end + if y > max_y then + max_y = y + end + end + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Triangulates the polygon. The polygon is divided into triangles. +-- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it +-- @return #table The triangles of the polygon +function POLYGON:Triangulate(points) + points = points or self.Points + local triangles = {} + + local function get_orientation(shape_points) + local sum = 0 + for i = 1, #shape_points do + local j = i % #shape_points + 1 + sum = sum + (shape_points[j].x - shape_points[i].x) * (shape_points[j].y + shape_points[i].y) + end + return sum >= 0 and "clockwise" or "counter-clockwise" -- sum >= 0, return "clockwise", else return "counter-clockwise" + end + + local function ensure_clockwise(shape_points) + local orientation = get_orientation(shape_points) + if orientation == "counter-clockwise" then + -- Reverse the order of shape_points so they're clockwise + local reversed = {} + for i = #shape_points, 1, -1 do + table.insert(reversed, shape_points[i]) + end + return reversed + end + return shape_points + end + + local function is_clockwise(p1, p2, p3) + local cross_product = (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x) + return cross_product < 0 + end + + local function divide_recursively(shape_points) + if #shape_points == 3 then + table.insert(triangles, TRIANGLE:New(shape_points[1], shape_points[2], shape_points[3])) + elseif #shape_points > 3 then -- find an ear -> a triangle with no other points inside it + for i, p1 in ipairs(shape_points) do + local p2 = shape_points[(i % #shape_points) + 1] + local p3 = shape_points[(i + 1) % #shape_points + 1] + local triangle = TRIANGLE:New(p1, p2, p3) + local is_ear = true + + if not is_clockwise(p1, p2, p3) then + is_ear = false + else + for _, point in ipairs(shape_points) do + if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then + is_ear = false + break + end + end + end + + if is_ear then + -- Check if any point in the original polygon is inside the ear triangle + local is_valid_triangle = true + for _, point in ipairs(points) do + if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then + is_valid_triangle = false + break + end + end + if is_valid_triangle then + table.insert(triangles, triangle) + local remaining_points = {} + for j, point in ipairs(shape_points) do + if point ~= p2 then + table.insert(remaining_points, point) + end + end + divide_recursively(remaining_points) + break + end + end + end + end + end + + points = ensure_clockwise(points) + divide_recursively(points) + return triangles +end + +function POLYGON:CovarianceMatrix() + local cx, cy = self:GetCentroid() + local covXX, covYY, covXY = 0, 0, 0 + for _, p in ipairs(self.points) do + covXX = covXX + (p.x - cx)^2 + covYY = covYY + (p.y - cy)^2 + covXY = covXY + (p.x - cx) * (p.y - cy) + end + covXX = covXX / (#self.points - 1) + covYY = covYY / (#self.points - 1) + covXY = covXY / (#self.points - 1) + return covXX, covYY, covXY +end + +function POLYGON:Direction() + local covXX, covYY, covXY = self:CovarianceMatrix() + -- Simplified calculation for the largest eigenvector's direction + local theta = 0.5 * math.atan2(2 * covXY, covXX - covYY) + return math.cos(theta), math.sin(theta) +end + +--- Returns a random Vec2 within the polygon. The Vec2 is weighted by the areas of the triangles that make up the polygon. +-- @return #table The random Vec2 +function POLYGON:GetRandomVec2() + local weights = {} + for _, triangle in pairs(self.Triangles) do + weights[triangle] = triangle.SurfaceArea / self.SurfaceArea + end + + local random_weight = math.random() + local accumulated_weight = 0 + for triangle, weight in pairs(weights) do + accumulated_weight = accumulated_weight + weight + if accumulated_weight >= random_weight then + return triangle:GetRandomVec2() + end + end +end + +--- Returns a random non-weighted Vec2 within the polygon. The Vec2 is chosen from one of the triangles that make up the polygon. +-- @return #table The random non-weighted Vec2 +function POLYGON:GetRandomNonWeightedVec2() + return self.Triangles[math.random(1, #self.Triangles)]:GetRandomVec2() +end + +--- Checks if a point is contained within the polygon. The point is a table with 'x' and 'y' fields. +-- @param #table point The point to check +-- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it +-- @return #bool True if the point is contained, false otherwise +function POLYGON:ContainsPoint(point, polygon_points) + local x = point.x + local y = point.y + + polygon_points = polygon_points or self.Points + + local counter = 0 + local num_points = #polygon_points + for current_index = 1, num_points do + local next_index = (current_index % num_points) + 1 + local current_x, current_y = polygon_points[current_index].x, polygon_points[current_index].y + local next_x, next_y = polygon_points[next_index].x, polygon_points[next_index].y + if ((current_y > y) ~= (next_y > y)) and (x < (next_x - current_x) * (y - current_y) / (next_y - current_y) + current_x) then + counter = counter + 1 + end + end + return counter % 2 == 1 +end + +--- Draws the polygon on the map. The polygon can be drawn with or without inner triangles. This is just for debugging +-- @param #bool include_inner_triangles Whether to include inner triangles in the drawing +function POLYGON:Draw(include_inner_triangles) + include_inner_triangles = include_inner_triangles or false + for i=1, #self.Coords do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.OutlineMarkIDs, c1:LineToAll(c2)) + end + + + if include_inner_triangles then + for _, triangle in ipairs(self.Triangles) do + triangle:Draw() + end + end +end + +--- Removes the drawing of the polygon from the map. +function POLYGON:RemoveDraw() + for _, triangle in pairs(self.Triangles) do + triangle:RemoveDraw() + end + for _, mark_id in pairs(self.OutlineMarkIDs) do + UTILS.RemoveMark(mark_id) + end +end + +--- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. +-- @return #number The surface area of the polygon +function POLYGON:__CalculateSurfaceArea() + local area = 0 + for _, triangle in pairs(self.Triangles) do + area = area + triangle.SurfaceArea + end + return area +end + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Moose Development/Moose/Shapes/ShapeBase.lua b/Moose Development/Moose/Shapes/ShapeBase.lua new file mode 100644 index 000000000..7042a44ad --- /dev/null +++ b/Moose Development/Moose/Shapes/ShapeBase.lua @@ -0,0 +1,216 @@ +--- **Shapes** - Class that serves as the base shapes drawn in the Mission Editor +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.SHAPE_BASE +-- @image CORE_Pathline.png + + +--- SHAPE_BASE class. +-- @type SHAPE_BASE +-- @field #string ClassName Name of the class. +-- @field #string Name Name of the shape +-- @field #table CenterVec2 Vec2 of the center of the shape, this will be assigned automatically +-- @field #table Points List of 3D points defining the shape, this will be assigned automatically +-- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically +-- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically +-- @extends Core.Base#BASE + +--- *I'm in love with the shape of you -- Ed Sheeran +-- +-- === +-- +-- # SHAPE_BASE +-- The class serves as the base class to deal with these shapes using MOOSE. You should never use this class on its own, +-- rather use: +-- CIRCLE +-- LINE +-- OVAL +-- POLYGON +-- TRIANGLE (although this one's a bit special as well) + +-- === +-- The idea is that anything you draw on the map in the Mission Editor can be turned in a shape to work with in MOOSE. +-- This is the base class that all other shape classes are built on. There are some shared functions, most of which are overridden in the derived classes + +-- @field #SHAPE_BASE + + +SHAPE_BASE = { + ClassName = "SHAPE_BASE", + Name = "", + CenterVec2 = nil, + Points = {}, + Coords = {}, + MarkIDs = {}, + ColorString = "", + ColorRGBA = {} +} + +--- Creates a new instance of SHAPE_BASE. +-- @return #SHAPE_BASE The new instance +function SHAPE_BASE:New() + local self = BASE:Inherit(self, BASE:New()) + return self +end + +--- Finds a shape on the map by its name. +-- @param #string shape_name Name of the shape to find +-- @return #SHAPE_BASE The found shape +function SHAPE_BASE:FindOnMap(shape_name) + local self = BASE:Inherit(self, BASE:New()) + + local found = false + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == shape_name then + self.Name = object["name"] + self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } + self.ColorString = object["colorString"] + self.ColorRGBA = UTILS.HexToRGBA(self.ColorString) + found = true + end + end + end + if not found then + self:E("Can't find a shape with name " .. shape_name) + end + return self +end + +function SHAPE_BASE:GetAllShapes(filter) + filter = filter or "" + local return_shapes = {} + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.contains(object["name"], filter) then + table.add(return_shapes, object) + end + end + end + + return return_shapes +end + +--- Offsets the shape to a new position. +-- @param #table new_vec2 The new position +function SHAPE_BASE:Offset(new_vec2) + local offset_vec2 = UTILS.Vec2Subtract(new_vec2, self.CenterVec2) + self.CenterVec2 = new_vec2 + if self.ClassName == "POLYGON" then + for _, point in pairs(self.Points) do + point.x = point.x + offset_vec2.x + point.y = point.y + offset_vec2.y + end + end +end + +--- Gets the name of the shape. +-- @return #string The name of the shape +function SHAPE_BASE:GetName() + return self.Name +end + +function SHAPE_BASE:GetColorString() + return self.ColorString +end + +function SHAPE_BASE:GetColorRGBA() + return self.ColorRGBA +end + +function SHAPE_BASE:GetColorRed() + return self.ColorRGBA.R +end + +function SHAPE_BASE:GetColorGreen() + return self.ColorRGBA.G +end + +function SHAPE_BASE:GetColorBlue() + return self.ColorRGBA.B +end + +function SHAPE_BASE:GetColorAlpha() + return self.ColorRGBA.A +end + +--- Gets the center position of the shape. +-- @return #table The center position +function SHAPE_BASE:GetCenterVec2() + return self.CenterVec2 +end + +--- Gets the center coordinate of the shape. +-- @return #COORDINATE The center coordinate +function SHAPE_BASE:GetCenterCoordinate() + return COORDINATE:NewFromVec2(self.CenterVec2) +end + +--- Gets the coordinate of the shape. +-- @return #COORDINATE The coordinate +function SHAPE_BASE:GetCoordinate() + return self:GetCenterCoordinate() +end + +--- Checks if a point is contained within the shape. +-- @param #table _ The point to check +-- @return #bool True if the point is contained, false otherwise +function SHAPE_BASE:ContainsPoint(_) + self:E("This needs to be set in the derived class") +end + +--- Checks if a unit is contained within the shape. +-- @param #string unit_name The name of the unit to check +-- @return #bool True if the unit is contained, false otherwise +function SHAPE_BASE:ContainsUnit(unit_name) + local unit = UNIT:FindByName(unit_name) + + if unit == nil or not unit:IsAlive() then + return false + end + + if self:ContainsPoint(unit:GetVec2()) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within the shape. +-- @param #string group_name The name of the group to check +-- @return #bool True if any unit of the group is contained, false otherwise +function SHAPE_BASE:ContainsAnyOfGroup(group_name) + local group = GROUP:FindByName(group_name) + + if group == nil or not group:IsAlive() then + return false + end + + for _, unit in pairs(group:GetUnits()) do + if self:ContainsPoint(unit:GetVec2()) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within the shape. +-- @param #string group_name The name of the group to check +-- @return #bool True if all units of the group are contained, false otherwise +function SHAPE_BASE:ContainsAllOfGroup(group_name) + local group = GROUP:FindByName(group_name) + + if group == nil or not group:IsAlive() then + return false + end + + for _, unit in pairs(group:GetUnits()) do + if not self:ContainsPoint(unit:GetVec2()) then + return false + end + end + return true +end diff --git a/Moose Development/Moose/Shapes/Triangle.lua b/Moose Development/Moose/Shapes/Triangle.lua new file mode 100644 index 000000000..c60b2aeef --- /dev/null +++ b/Moose Development/Moose/Shapes/Triangle.lua @@ -0,0 +1,86 @@ +-- TRIANGLE class with properties and methods for handling triangles. This class is mostly used by the POLYGON class, but you can use it on its own as well +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- +TRIANGLE = { + ClassName = "TRIANGLE", + Points = {}, + Coords = {}, + SurfaceArea = 0 +} + +--- Creates a new triangle from three points. The points need to be given as Vec2s +-- @param #table p1 The first point of the triangle +-- @param #table p2 The second point of the triangle +-- @param #table p3 The third point of the triangle +-- @return #TRIANGLE The new triangle +function TRIANGLE:New(p1, p2, p3) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.Points = {p1, p2, p3} + + local center_x = (p1.x + p2.x + p3.x) / 3 + local center_y = (p1.y + p2.y + p3.y) / 3 + self.CenterVec2 = {x=center_x, y=center_y} + + for _, pt in pairs({p1, p2, p3}) do + table.add(self.Coords, COORDINATE:NewFromVec2(pt)) + end + + self.SurfaceArea = math.abs((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) * 0.5 + + self.MarkIDs = {} + return self +end + +--- Checks if a point is contained within the triangle. +-- @param #table pt The point to check +-- @param #table points (optional) The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it +-- @return #bool True if the point is contained, false otherwise +function TRIANGLE:ContainsPoint(pt, points) + points = points or self.Points + + local function sign(p1, p2, p3) + return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y) + end + + local d1 = sign(pt, self.Points[1], self.Points[2]) + local d2 = sign(pt, self.Points[2], self.Points[3]) + local d3 = sign(pt, self.Points[3], self.Points[1]) + + local has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0) + local has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0) + + return not (has_neg and has_pos) +end + +--- Returns a random Vec2 within the triangle. +-- @param #table points The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it +-- @return #table The random Vec2 +function TRIANGLE:GetRandomVec2(points) + points = points or self.Points + local pt = {math.random(), math.random()} + table.sort(pt) + local s = pt[1] + local t = pt[2] - pt[1] + local u = 1 - pt[2] + + return {x = s * points[1].x + t * points[2].x + u * points[3].x, + y = s * points[1].y + t * points[2].y + u * points[3].y} +end + +--- Draws the triangle on the map, just for debugging +function TRIANGLE:Draw() + for i=1, #self.Coords do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.MarkIDs, c1:LineToAll(c2)) + end +end + +--- Removes the drawing of the triangle from the map. +function TRIANGLE:RemoveDraw() + for _, mark_id in pairs(self.MarkIDs) do + UTILS.RemoveMark(mark_id) + end +end diff --git a/Moose Development/Moose/Utilities/Utils.lua b/Moose Development/Moose/Utilities/Utils.lua index 6bf7e1fc9..96cf8c1b4 100644 --- a/Moose Development/Moose/Utilities/Utils.lua +++ b/Moose Development/Moose/Utilities/Utils.lua @@ -3513,6 +3513,25 @@ function string.contains(str, value) return string.match(str, value) end + +--- Moves an object from one table to another +-- @param #obj object to move +-- @param #from_table table to move from +-- @param #to_table table to move to +function table.move_object(obj, from_table, to_table) + local index + for i, v in pairs(from_table) do + if v == obj then + index = i + end + end + + if index then + local moved = table.remove(from_table, index) + table.insert_unique(to_table, moved) + end +end + --- Given tbl is a indexed table ({"hello", "dcs", "world"}), checks if element exists in the table. --- The table can be made up out of complex tables or values as well -- @param #table tbl @@ -3731,6 +3750,25 @@ function UTILS.OctalToDecimal(Number) return tonumber(Number,8) end + +--- HexToRGBA +-- @param hex_string table +-- @return #table R, G, B, A +function UTILS.HexToRGBA(hex_string) + local hexNumber = tonumber(string.sub(hex_string, 3), 16) -- convert the string to a number + -- extract RGBA components + local alpha = hexNumber % 256 + hexNumber = (hexNumber - alpha) / 256 + local blue = hexNumber % 256 + hexNumber = (hexNumber - blue) / 256 + local green = hexNumber % 256 + hexNumber = (hexNumber - green) / 256 + local red = hexNumber % 256 + + return {R = red, G = green, B = blue, A = alpha} +end + + --- Function to save the position of a set of #OPSGROUP (ARMYGROUP) objects. -- @param Core.Set#SET_OPSGROUP Set of ops objects to save -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. @@ -3768,7 +3806,7 @@ function UTILS.SaveSetOfOpsGroups(Set,Path,Filename,Structured) data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d,%s\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z,strucdata) else data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z) - end + end end end -- save the data @@ -3780,12 +3818,12 @@ end -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. -- @param #string Filename The name of the file. -- @return #table Returns a table of data entries: `{ groupname=groupname, size=size, coordinate=coordinate, template=template, structure=structure, legion=legion, alttemplate=alttemplate }` --- Returns nil when the file cannot be read. +-- Returns nil when the file cannot be read. function UTILS.LoadSetOfOpsGroups(Path,Filename) local filename = Filename or "SetOfGroups" local datatable = {} - + if UTILS.CheckFileExists(Path,filename) then local outcome,loadeddata = UTILS.LoadFromFile(Path,Filename) -- remove header @@ -3820,20 +3858,20 @@ end -- @param #number tgtHdg The absolute heading from the reference object to the target object/point in 0-360 -- @return #string text Text in clock heading such as "4 O'CLOCK" -- @usage Display the range and clock distance of a BTR in relation to REAPER 1-1's heading: --- +-- -- myUnit = UNIT:FindByName( "REAPER 1-1" ) -- myTarget = GROUP:FindByName( "BTR-1" ) --- +-- -- coordUnit = myUnit:GetCoordinate() -- coordTarget = myTarget:GetCoordinate() --- +-- -- hdgUnit = myUnit:GetHeading() -- hdgTarget = coordUnit:HeadingTo( coordTarget ) -- distTarget = coordUnit:Get3DDistance( coordTarget ) --- +-- -- clockString = UTILS.ClockHeadingString( hdgUnit, hdgTarget ) --- --- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! +-- +-- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! -- MESSAGE:New("Contact BTR at " .. clockString .. " for " .. distTarget .. "m!):ToUnit( myUnit ) function UTILS.ClockHeadingString(refHdg,tgtHdg) local relativeAngle = tgtHdg - refHdg From 28411d20937ffa8914e4d83e317f4ab3eef26fa4 Mon Sep 17 00:00:00 2001 From: Thomas <72444570+Applevangelist@users.noreply.github.com> Date: Sun, 21 Apr 2024 10:12:51 +0200 Subject: [PATCH 11/15] Revert "Adding SHAPES (#2110)" (#2112) This reverts commit 26deaca16632a2e16a854339f32170f0594f717d. --- Moose Development/Moose/Modules.lua | 9 - Moose Development/Moose/Shapes/Circle.lua | 259 ----------- Moose Development/Moose/Shapes/Cube.lua | 66 --- Moose Development/Moose/Shapes/Line.lua | 331 -------------- Moose Development/Moose/Shapes/Oval.lua | 213 --------- Moose Development/Moose/Shapes/Polygon.lua | 458 ------------------- Moose Development/Moose/Shapes/ShapeBase.lua | 216 --------- Moose Development/Moose/Shapes/Triangle.lua | 86 ---- Moose Development/Moose/Utilities/Utils.lua | 56 +-- 9 files changed, 9 insertions(+), 1685 deletions(-) delete mode 100644 Moose Development/Moose/Shapes/Circle.lua delete mode 100644 Moose Development/Moose/Shapes/Cube.lua delete mode 100644 Moose Development/Moose/Shapes/Line.lua delete mode 100644 Moose Development/Moose/Shapes/Oval.lua delete mode 100644 Moose Development/Moose/Shapes/Polygon.lua delete mode 100644 Moose Development/Moose/Shapes/ShapeBase.lua delete mode 100644 Moose Development/Moose/Shapes/Triangle.lua diff --git a/Moose Development/Moose/Modules.lua b/Moose Development/Moose/Modules.lua index c95366e45..c483b8d21 100644 --- a/Moose Development/Moose/Modules.lua +++ b/Moose Development/Moose/Modules.lua @@ -122,15 +122,6 @@ __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Route.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Account.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Assist.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/ShapeBase.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Circle.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Cube.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Line.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Oval.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Polygon.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Triangle.lua' ) -__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Arrow.lua' ) - __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/UserSound.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/SoundOutput.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/Radio.lua' ) diff --git a/Moose Development/Moose/Shapes/Circle.lua b/Moose Development/Moose/Shapes/Circle.lua deleted file mode 100644 index 04c153d86..000000000 --- a/Moose Development/Moose/Shapes/Circle.lua +++ /dev/null @@ -1,259 +0,0 @@ --- --- --- ### Author: **nielsvaes/coconutcockpit** --- --- === --- @module Shapes.CIRCLE - ---- CIRCLE class. --- @type CIRCLE --- @field #string ClassName Name of the class. --- @field #number Radius Radius of the circle - ---- *It's NOT hip to be square* -- Someone, somewhere, probably --- --- === --- --- # CIRCLE --- CIRCLEs can be fetched from the drawings in the Mission Editor - --- This class has some of the standard CIRCLE functions you'd expect. One function of interest is CIRCLE:PointInSector() that you can use if a point is --- within a certain sector (pizza slice) of a circle. This can be useful for many things, including rudimentary, "radar-like" searches from a unit. - --- @field #CIRCLE - ---- CIRCLE class with properties and methods for handling circles. -CIRCLE = { - ClassName = "CIRCLE", - Radius = nil, -} ---- Finds a circle on the map by its name. The circle must have been added in the Mission Editor --- @param #string shape_name Name of the circle to find --- @return #CIRCLE The found circle, or nil if not found -function CIRCLE:FindOnMap(shape_name) - local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) - for _, layer in pairs(env.mission.drawings.layers) do - for _, object in pairs(layer["objects"]) do - if string.find(object["name"], shape_name, 1, true) then - if object["polygonMode"] == "circle" then - self.Radius = object["radius"] - end - end - end - end - - return self -end - ---- Finds a circle by its name in the database. --- @param #string shape_name Name of the circle to find --- @return #CIRCLE The found circle, or nil if not found -function CIRCLE:Find(shape_name) - return _DATABASE:FindShape(shape_name) -end - ---- Creates a new circle from a center point and a radius. --- @param #table vec2 The center point of the circle --- @param #number radius The radius of the circle --- @return #CIRCLE The new circle -function CIRCLE:New(vec2, radius) - local self = BASE:Inherit(self, SHAPE_BASE:New()) - self.CenterVec2 = vec2 - self.Radius = radius - return self -end - ---- Gets the radius of the circle. --- @return #number The radius of the circle -function CIRCLE:GetRadius() - return self.Radius -end - ---- Checks if a point is contained within the circle. --- @param #table point The point to check --- @return #bool True if the point is contained, false otherwise -function CIRCLE:ContainsPoint(point) - if ((point.x - self.CenterVec2.x) ^ 2 + (point.y - self.CenterVec2.y) ^ 2) ^ 0.5 <= self.Radius then - return true - end - return false -end - ---- Checks if a point is contained within a sector of the circle. The start and end sector need to be clockwise --- @param #table point The point to check --- @param #table sector_start The start point of the sector --- @param #table sector_end The end point of the sector --- @param #table center The center point of the sector --- @param #number radius The radius of the sector --- @return #bool True if the point is contained, false otherwise -function CIRCLE:PointInSector(point, sector_start, sector_end, center, radius) - center = center or self.CenterVec2 - radius = radius or self.Radius - - local function are_clockwise(v1, v2) - return -v1.x * v2.y + v1.y * v2.x > 0 - end - - local function is_in_radius(rp) - return rp.x * rp.x + rp.y * rp.y <= radius ^ 2 - end - - local rel_pt = { - x = point.x - center.x, - y = point.y - center.y - } - - local rel_sector_start = { - x = sector_start.x - center.x, - y = sector_start.y - center.y, - } - - local rel_sector_end = { - x = sector_end.x - center.x, - y = sector_end.y - center.y, - } - - return not are_clockwise(rel_sector_start, rel_pt) and - are_clockwise(rel_sector_end, rel_pt) and - is_in_radius(rel_pt, radius) -end - ---- Checks if a unit is contained within a sector of the circle. The start and end sector need to be clockwise --- @param #string unit_name The name of the unit to check --- @param #table sector_start The start point of the sector --- @param #table sector_end The end point of the sector --- @param #table center The center point of the sector --- @param #number radius The radius of the sector --- @return #bool True if the unit is contained, false otherwise -function CIRCLE:UnitInSector(unit_name, sector_start, sector_end, center, radius) - center = center or self.CenterVec2 - radius = radius or self.Radius - - if self:PointInSector(UNIT:FindByName(unit_name):GetVec2(), sector_start, sector_end, center, radius) then - return true - end - return false -end - ---- Checks if any unit of a group is contained within a sector of the circle. The start and end sector need to be clockwise --- @param #string group_name The name of the group to check --- @param #table sector_start The start point of the sector --- @param #table sector_end The end point of the sector --- @param #table center The center point of the sector --- @param #number radius The radius of the sector --- @return #bool True if any unit of the group is contained, false otherwise -function CIRCLE:AnyOfGroupInSector(group_name, sector_start, sector_end, center, radius) - center = center or self.CenterVec2 - radius = radius or self.Radius - - for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do - if self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then - return true - end - end - return false -end - ---- Checks if all units of a group are contained within a sector of the circle. The start and end sector need to be clockwise --- @param #string group_name The name of the group to check --- @param #table sector_start The start point of the sector --- @param #table sector_end The end point of the sector --- @param #table center The center point of the sector --- @param #number radius The radius of the sector --- @return #bool True if all units of the group are contained, false otherwise -function CIRCLE:AllOfGroupInSector(group_name, sector_start, sector_end, center, radius) - center = center or self.CenterVec2 - radius = radius or self.Radius - - for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do - if not self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then - return false - end - end - return true -end - ---- Checks if a unit is contained within a radius of the circle. --- @param #string unit_name The name of the unit to check --- @param #table center The center point of the radius --- @param #number radius The radius to check --- @return #bool True if the unit is contained, false otherwise -function CIRCLE:UnitInRadius(unit_name, center, radius) - center = center or self.CenterVec2 - radius = radius or self.Radius - - if UTILS.IsInRadius(center, UNIT:FindByName(unit_name):GetVec2(), radius) then - return true - end - return false -end - ---- Checks if any unit of a group is contained within a radius of the circle. --- @param #string group_name The name of the group to check --- @param #table center The center point of the radius --- @param #number radius The radius to check --- @return #bool True if any unit of the group is contained, false otherwise -function CIRCLE:AnyOfGroupInRadius(group_name, center, radius) - center = center or self.CenterVec2 - radius = radius or self.Radius - - for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do - if UTILS.IsInRadius(center, unit:GetVec2(), radius) then - return true - end - end - return false -end - ---- Checks if all units of a group are contained within a radius of the circle. --- @param #string group_name The name of the group to check --- @param #table center The center point of the radius --- @param #number radius The radius to check --- @return #bool True if all units of the group are contained, false otherwise -function CIRCLE:AllOfGroupInRadius(group_name, center, radius) - center = center or self.CenterVec2 - radius = radius or self.Radius - - for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do - if not UTILS.IsInRadius(center, unit:GetVec2(), radius) then - return false - end - end - return true -end - ---- Returns a random Vec2 within the circle. --- @return #table The random Vec2 -function CIRCLE:GetRandomVec2() - local angle = math.random() * 2 * math.pi - - local rx = math.random(0, self.Radius) * math.cos(angle) + self.CenterVec2.x - local ry = math.random(0, self.Radius) * math.sin(angle) + self.CenterVec2.y - - return {x=rx, y=ry} -end - ---- Returns a random Vec2 on the border of the circle. --- @return #table The random Vec2 -function CIRCLE:GetRandomVec2OnBorder() - local angle = math.random() * 2 * math.pi - - local rx = self.Radius * math.cos(angle) + self.CenterVec2.x - local ry = self.Radius * math.sin(angle) + self.CenterVec2.y - - return {x=rx, y=ry} -end - ---- Calculates the bounding box of the circle. The bounding box is the smallest rectangle that contains the circle. --- @return #table The bounding box of the circle -function CIRCLE:GetBoundingBox() - local min_x = self.CenterVec2.x - self.Radius - local min_y = self.CenterVec2.y - self.Radius - local max_x = self.CenterVec2.x + self.Radius - local max_y = self.CenterVec2.y + self.Radius - - return { - {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} - } -end - diff --git a/Moose Development/Moose/Shapes/Cube.lua b/Moose Development/Moose/Shapes/Cube.lua deleted file mode 100644 index ae3f73090..000000000 --- a/Moose Development/Moose/Shapes/Cube.lua +++ /dev/null @@ -1,66 +0,0 @@ -CUBE = { - ClassName = "CUBE", - Points = {}, - Coords = {} -} - ---- Points need to be added in the following order: ---- p1 -> p4 make up the front face of the cube ---- p5 -> p8 make up the back face of the cube ---- p1 connects to p5 ---- p2 connects to p6 ---- p3 connects to p7 ---- p4 connects to p8 ---- ---- 8-----------7 ---- /| /| ---- / | / | ---- 4--+--------3 | ---- | | | | ---- | | | | ---- | | | | ---- | 5--------+--6 ---- | / | / ---- |/ |/ ---- 1-----------2 ---- -function CUBE:New(p1, p2, p3, p4, p5, p6, p7, p8) - local self = BASE:Inherit(self, SHAPE_BASE) - self.Points = {p1, p2, p3, p4, p5, p6, p7, p8} - for _, point in spairs(self.Points) do - table.insert(self.Coords, COORDINATE:NewFromVec3(point)) - end - return self -end - -function CUBE:GetCenter() - local center = { x=0, y=0, z=0 } - for _, point in pairs(self.Points) do - center.x = center.x + point.x - center.y = center.y + point.y - center.z = center.z + point.z - end - - center.x = center.x / 8 - center.y = center.y / 8 - center.z = center.z / 8 - return center -end - -function CUBE:ContainsPoint(point, cube_points) - cube_points = cube_points or self.Points - local min_x, min_y, min_z = math.huge, math.huge, math.huge - local max_x, max_y, max_z = -math.huge, -math.huge, -math.huge - - -- Find the minimum and maximum x, y, and z values of the cube points - for _, p in ipairs(cube_points) do - if p.x < min_x then min_x = p.x end - if p.y < min_y then min_y = p.y end - if p.z < min_z then min_z = p.z end - if p.x > max_x then max_x = p.x end - if p.y > max_y then max_y = p.y end - if p.z > max_z then max_z = p.z end - end - - return point.x >= min_x and point.x <= max_x and point.y >= min_y and point.y <= max_y and point.z >= min_z and point.z <= max_z -end diff --git a/Moose Development/Moose/Shapes/Line.lua b/Moose Development/Moose/Shapes/Line.lua deleted file mode 100644 index 08f7c84a0..000000000 --- a/Moose Development/Moose/Shapes/Line.lua +++ /dev/null @@ -1,331 +0,0 @@ --- --- --- ### Author: **nielsvaes/coconutcockpit** --- --- === --- @module Shapes.LINE - ---- OVAL class. --- @type OVAL --- @field #string ClassName Name of the class. --- @field #number Points points of the line --- @field #number Coords coordinates of the line - --- --- === - --- @field #LINE -LINE = { - ClassName = "LINE", - Points = {}, - Coords = {}, -} - ---- Finds a line on the map by its name. The line must be drawn in the Mission Editor --- @param #string line_name Name of the line to find --- @return #LINE The found line, or nil if not found -function LINE:FindOnMap(line_name) - local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(line_name)) - - for _, layer in pairs(env.mission.drawings.layers) do - for _, object in pairs(layer["objects"]) do - if object["name"] == line_name then - if object["primitiveType"] == "Line" then - for _, point in UTILS.spairs(object["points"]) do - local p = {x = object["mapX"] + point["x"], - y = object["mapY"] + point["y"] } - local coord = COORDINATE:NewFromVec2(p) - table.insert(self.Points, p) - table.insert(self.Coords, coord) - end - end - end - end - end - - self:I(#self.Points) - if #self.Points == 0 then - return nil - end - - self.MarkIDs = {} - - return self -end - ---- Finds a line by its name in the database. --- @param #string shape_name Name of the line to find --- @return #LINE The found line, or nil if not found -function LINE:Find(shape_name) - return _DATABASE:FindShape(shape_name) -end - ---- Creates a new line from two points. --- @param #table vec2 The first point of the line --- @param #number radius The second point of the line --- @return #LINE The new line -function LINE:New(...) - local self = BASE:Inherit(self, SHAPE_BASE:New()) - self.Points = {...} - self:I(self.Points) - for _, point in UTILS.spairs(self.Points) do - table.insert(self.Coords, COORDINATE:NewFromVec2(point)) - end - return self -end - ---- Creates a new line from a circle. --- @param #table center_point center point of the circle --- @param #number radius radius of the circle, half length of the line --- @param #number angle_degrees degrees the line will form from center point --- @return #LINE The new line -function LINE:NewFromCircle(center_point, radius, angle_degrees) - local self = BASE:Inherit(self, SHAPE_BASE:New()) - self.CenterVec2 = center_point - local angleRadians = math.rad(angle_degrees) - - local point1 = { - x = center_point.x + radius * math.cos(angleRadians), - y = center_point.y + radius * math.sin(angleRadians) - } - - local point2 = { - x = center_point.x + radius * math.cos(angleRadians + math.pi), - y = center_point.y + radius * math.sin(angleRadians + math.pi) - } - - for _, point in pairs{point1, point2} do - table.insert(self.Points, point) - table.insert(self.Coords, COORDINATE:NewFromVec2(point)) - end - - return self -end - ---- Gets the coordinates of the line. --- @return #table The coordinates of the line -function LINE:Coordinates() - return self.Coords -end - ---- Gets the start coordinate of the line. The start coordinate is the first point of the line. --- @return #COORDINATE The start coordinate of the line -function LINE:GetStartCoordinate() - return self.Coords[1] -end - ---- Gets the end coordinate of the line. The end coordinate is the last point of the line. --- @return #COORDINATE The end coordinate of the line -function LINE:GetEndCoordinate() - return self.Coords[#self.Coords] -end - ---- Gets the start point of the line. The start point is the first point of the line. --- @return #table The start point of the line -function LINE:GetStartPoint() - return self.Points[1] -end - ---- Gets the end point of the line. The end point is the last point of the line. --- @return #table The end point of the line -function LINE:GetEndPoint() - return self.Points[#self.Points] -end - ---- Gets the length of the line. --- @return #number The length of the line -function LINE:GetLength() - local total_length = 0 - for i=1, #self.Points - 1 do - local x1, y1 = self.Points[i]["x"], self.Points[i]["y"] - local x2, y2 = self.Points[i+1]["x"], self.Points[i+1]["y"] - local segment_length = math.sqrt((x2 - x1)^2 + (y2 - y1)^2) - total_length = total_length + segment_length - end - return total_length -end - ---- Returns a random point on the line. --- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it --- @return #table The random point -function LINE:GetRandomPoint(points) - points = points or self.Points - local rand = math.random() -- 0->1 - - local random_x = points[1].x + rand * (points[2].x - points[1].x) - local random_y = points[1].y + rand * (points[2].y - points[1].y) - - return { x= random_x, y= random_y } -end - ---- Gets the heading of the line. --- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it --- @return #number The heading of the line -function LINE:GetHeading(points) - points = points or self.Points - - local angle = math.atan2(points[2].y - points[1].y, points[2].x - points[1].x) - - angle = math.deg(angle) - if angle < 0 then - angle = angle + 360 - end - - return angle -end - - ---- Return each part of the line as a new line --- @return #table The points -function LINE:GetIndividualParts() - local parts = {} - if #self.Points == 2 then - parts = {self} - end - - for i=1, #self.Points -1 do - local p1 = self.Points[i] - local p2 = self.Points[i % #self.Points + 1] - table.add(parts, LINE:New(p1, p2)) - end - - return parts -end - ---- Gets a number of points in between the start and end points of the line. --- @param #number amount The number of points to get --- @param #table start_point (Optional) The start point of the line, defaults to the object's start point --- @param #table end_point (Optional) The end point of the line, defaults to the object's end point --- @return #table The points -function LINE:GetPointsInbetween(amount, start_point, end_point) - start_point = start_point or self:GetStartPoint() - end_point = end_point or self:GetEndPoint() - if amount == 0 then return {start_point, end_point} end - - amount = amount + 1 - local points = {} - - local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } - local divided = { x = difference.x / amount, y = difference.y / amount } - - for j=0, amount do - local part_pos = {x = divided.x * j, y = divided.y * j} - -- add part_pos vector to the start point so the new point is placed along in the line - local point = {x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} - table.insert(points, point) - end - return points -end - ---- Gets a number of points in between the start and end points of the line. --- @param #number amount The number of points to get --- @param #table start_point (Optional) The start point of the line, defaults to the object's start point --- @param #table end_point (Optional) The end point of the line, defaults to the object's end point --- @return #table The points -function LINE:GetCoordinatesInBetween(amount, start_point, end_point) - local coords = {} - for _, pt in pairs(self:GetPointsInbetween(amount, start_point, end_point)) do - table.add(coords, COORDINATE:NewFromVec2(pt)) - end - return coords -end - - -function LINE:GetRandomPoint(start_point, end_point) - start_point = start_point or self:GetStartPoint() - end_point = end_point or self:GetEndPoint() - - local fraction = math.random() - - local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } - local part_pos = {x = difference.x * fraction, y = difference.y * fraction} - local random_point = { x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} - - return random_point -end - - -function LINE:GetRandomCoordinate(start_point, end_point) - start_point = start_point or self:GetStartPoint() - end_point = end_point or self:GetEndPoint() - - return COORDINATE:NewFromVec2(self:GetRandomPoint(start_point, end_point)) -end - - ---- Gets a number of points on a sine wave between the start and end points of the line. --- @param #number amount The number of points to get --- @param #table start_point (Optional) The start point of the line, defaults to the object's start point --- @param #table end_point (Optional) The end point of the line, defaults to the object's end point --- @param #number frequency (Optional) The frequency of the sine wave, default 1 --- @param #number phase (Optional) The phase of the sine wave, default 0 --- @param #number amplitude (Optional) The amplitude of the sine wave, default 100 --- @return #table The points -function LINE:GetPointsBetweenAsSineWave(amount, start_point, end_point, frequency, phase, amplitude) - amount = amount or 20 - start_point = start_point or self:GetStartPoint() - end_point = end_point or self:GetEndPoint() - frequency = frequency or 1 -- number of cycles per unit of x - phase = phase or 0 -- offset in radians - amplitude = amplitude or 100 -- maximum height of the wave - - local points = {} - - -- Returns the y-coordinate of the sine wave at x - local function sine_wave(x) - return amplitude * math.sin(2 * math.pi * frequency * (x - start_point.x) + phase) - end - - -- Plot x-amount of points on the sine wave between point_01 and point_02 - local x = start_point.x - local step = (end_point.x - start_point.x) / 20 - for _=1, amount do - local y = sine_wave(x) - x = x + step - table.add(points, {x=x, y=y}) - end - return points -end - ---- Calculates the bounding box of the line. The bounding box is the smallest rectangle that contains the line. --- @return #table The bounding box of the line -function LINE:GetBoundingBox() - local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[2].x, self.Points[2].y - - for i = 2, #self.Points do - local x, y = self.Points[i].x, self.Points[i].y - - if x < min_x then - min_x = x - end - if y < min_y then - min_y = y - end - if x > max_x then - max_x = x - end - if y > max_y then - max_y = y - end - end - return { - {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} - } -end - ---- Draws the line on the map. --- @param #table points The points of the line -function LINE:Draw() - for i=1, #self.Coords -1 do - local c1 = self.Coords[i] - local c2 = self.Coords[i % #self.Coords + 1] - table.add(self.MarkIDs, c1:LineToAll(c2)) - end -end - ---- Removes the drawing of the line from the map. -function LINE:RemoveDraw() - for _, mark_id in pairs(self.MarkIDs) do - UTILS.RemoveMark(mark_id) - end -end diff --git a/Moose Development/Moose/Shapes/Oval.lua b/Moose Development/Moose/Shapes/Oval.lua deleted file mode 100644 index d2f85a822..000000000 --- a/Moose Development/Moose/Shapes/Oval.lua +++ /dev/null @@ -1,213 +0,0 @@ --- --- --- ### Author: **nielsvaes/coconutcockpit** --- --- === --- @module Shapes.OVAL - ---- OVAL class. --- @type OVAL --- @field #string ClassName Name of the class. --- @field #number MajorAxis The major axis (radius) of the oval --- @field #number MinorAxis The minor axis (radius) of the oval --- @field #number Angle The angle the oval is rotated on - ---- *The little man removed his hat, what an egg shaped head he had* -- Agatha Christie --- --- === --- --- # OVAL --- OVALs can be fetched from the drawings in the Mission Editor - --- The major and minor axes define how elongated the shape of an oval is. This class has some basic functions that the other SHAPE classes have as well. --- Since it's not possible to draw the shape of an oval while the mission is running, right now the draw function draws 2 cicles. One with the major axis and one with --- the minor axis. It then draws a diamond shape on an angle where the corners touch the major and minor axes to give an indication of what the oval actually --- looks like. - --- Using ovals can be handy to find an area on the ground that is actually an intersection of a cone and a plane. So imagine you're faking the view cone of --- a targeting pod and - --- @field #OVAL - ---- OVAL class with properties and methods for handling ovals. -OVAL = { - ClassName = "OVAL", - MajorAxis = nil, - MinorAxis = nil, - Angle = 0, - DrawPoly=nil -} - ---- Finds an oval on the map by its name. The oval must be drawn on the map. --- @param #string shape_name Name of the oval to find --- @return #OVAL The found oval, or nil if not found -function OVAL:FindOnMap(shape_name) - local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) - for _, layer in pairs(env.mission.drawings.layers) do - for _, object in pairs(layer["objects"]) do - if string.find(object["name"], shape_name, 1, true) then - if object["polygonMode"] == "oval" then - self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } - self.MajorAxis = object["r1"] - self.MinorAxis = object["r2"] - self.Angle = object["angle"] - end - end - end - end - - return self -end - ---- Finds an oval by its name in the database. --- @param #string shape_name Name of the oval to find --- @return #OVAL The found oval, or nil if not found -function OVAL:Find(shape_name) - return _DATABASE:FindShape(shape_name) -end - ---- Creates a new oval from a center point, major axis, minor axis, and angle. --- @param #table vec2 The center point of the oval --- @param #number major_axis The major axis of the oval --- @param #number minor_axis The minor axis of the oval --- @param #number angle The angle of the oval --- @return #OVAL The new oval -function OVAL:New(vec2, major_axis, minor_axis, angle) - local self = BASE:Inherit(self, SHAPE_BASE:New()) - self.CenterVec2 = vec2 - self.MajorAxis = major_axis - self.MinorAxis = minor_axis - self.Angle = angle or 0 - - return self -end - ---- Gets the major axis of the oval. --- @return #number The major axis of the oval -function OVAL:GetMajorAxis() - return self.MajorAxis -end - ---- Gets the minor axis of the oval. --- @return #number The minor axis of the oval -function OVAL:GetMinorAxis() - return self.MinorAxis -end - ---- Gets the angle of the oval. --- @return #number The angle of the oval -function OVAL:GetAngle() - return self.Angle -end - ---- Sets the major axis of the oval. --- @param #number value The new major axis -function OVAL:SetMajorAxis(value) - self.MajorAxis = value -end - ---- Sets the minor axis of the oval. --- @param #number value The new minor axis -function OVAL:SetMinorAxis(value) - self.MinorAxis = value -end - ---- Sets the angle of the oval. --- @param #number value The new angle -function OVAL:SetAngle(value) - self.Angle = value -end - ---- Checks if a point is contained within the oval. --- @param #table point The point to check --- @return #bool True if the point is contained, false otherwise -function OVAL:ContainsPoint(point) - local cos, sin = math.cos, math.sin - local dx = point.x - self.CenterVec2.x - local dy = point.y - self.CenterVec2.y - local rx = dx * cos(self.Angle) + dy * sin(self.Angle) - local ry = -dx * sin(self.Angle) + dy * cos(self.Angle) - return rx * rx / (self.MajorAxis * self.MajorAxis) + ry * ry / (self.MinorAxis * self.MinorAxis) <= 1 -end - ---- Returns a random Vec2 within the oval. --- @return #table The random Vec2 -function OVAL:GetRandomVec2() - local theta = math.rad(self.Angle) - - local random_point = math.sqrt(math.random()) --> uniformly - --local random_point = math.random() --> more clumped around center - local phi = math.random() * 2 * math.pi - local x_c = random_point * math.cos(phi) - local y_c = random_point * math.sin(phi) - local x_e = x_c * self.MajorAxis - local y_e = y_c * self.MinorAxis - local rx = (x_e * math.cos(theta) - y_e * math.sin(theta)) + self.CenterVec2.x - local ry = (x_e * math.sin(theta) + y_e * math.cos(theta)) + self.CenterVec2.y - - return {x=rx, y=ry} -end - ---- Calculates the bounding box of the oval. The bounding box is the smallest rectangle that contains the oval. --- @return #table The bounding box of the oval -function OVAL:GetBoundingBox() - local min_x = self.CenterVec2.x - self.MajorAxis - local min_y = self.CenterVec2.y - self.MinorAxis - local max_x = self.CenterVec2.x + self.MajorAxis - local max_y = self.CenterVec2.y + self.MinorAxis - - return { - {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} - } -end - ---- Draws the oval on the map, for debugging --- @param #number angle (Optional) The angle of the oval. If nil will use self.Angle -function OVAL:Draw() - --for pt in pairs(self:PointsOnEdge(20)) do - -- COORDINATE:NewFromVec2(pt) - --end - - self.DrawPoly = POLYGON:NewFromPoints(self:PointsOnEdge(20)) - self.DrawPoly:Draw(true) - - - - - ---- TODO: draw a better shape using line segments - --angle = angle or self.Angle - --local coor = self:GetCenterCoordinate() - -- - --table.add(self.MarkIDs, coor:CircleToAll(self.MajorAxis)) - --table.add(self.MarkIDs, coor:CircleToAll(self.MinorAxis)) - --table.add(self.MarkIDs, coor:LineToAll(coor:Translate(self.MajorAxis, self.Angle))) - -- - --local pt_1 = coor:Translate(self.MajorAxis, self.Angle) - --local pt_2 = coor:Translate(self.MinorAxis, self.Angle - 90) - --local pt_3 = coor:Translate(self.MajorAxis, self.Angle - 180) - --local pt_4 = coor:Translate(self.MinorAxis, self.Angle - 270) - --table.add(self.MarkIDs, pt_1:QuadToAll(pt_2, pt_3, pt_4), -1, {0, 1, 0}, 1, {0, 1, 0}) -end - ---- Removes the drawing of the oval from the map -function OVAL:RemoveDraw() - self.DrawPoly:RemoveDraw() -end - - -function OVAL:PointsOnEdge(num_points) - num_points = num_points or 20 - local points = {} - local dtheta = 2 * math.pi / num_points - - for i = 0, num_points - 1 do - local theta = i * dtheta - local x = self.CenterVec2.x + self.MajorAxis * math.cos(theta) * math.cos(self.Angle) - self.MinorAxis * math.sin(theta) * math.sin(self.Angle) - local y = self.CenterVec2.y + self.MajorAxis * math.cos(theta) * math.sin(self.Angle) + self.MinorAxis * math.sin(theta) * math.cos(self.Angle) - table.insert(points, {x = x, y = y}) - end - - return points -end - - diff --git a/Moose Development/Moose/Shapes/Polygon.lua b/Moose Development/Moose/Shapes/Polygon.lua deleted file mode 100644 index a40256ecf..000000000 --- a/Moose Development/Moose/Shapes/Polygon.lua +++ /dev/null @@ -1,458 +0,0 @@ --- --- --- ### Author: **nielsvaes/coconutcockpit** --- --- === --- @module Shapes.POLYGON - ---- POLYGON class. --- @type POLYGON --- @field #string ClassName Name of the class. --- @field #table Points List of 3D points defining the shape, this will be assigned automatically if you're passing in a drawing from the Mission Editor --- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically if you're passing in a drawing from the Mission Editor --- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically if you're passing in a drawing from the Mission Editor --- @field #table Triangles List of TRIANGLEs that make up the shape of the POLYGON after being triangulated --- @extends Core.Base#BASE - ---- *Polygons are fashionable at the moment* -- Trip Hawkins --- --- === --- --- # POLYGON --- POLYGONs can be fetched from the drawings in the Mission Editor if the drawing is: --- * A closed shape made with line segments --- * A closed shape made with a freehand line --- * A freehand drawn polygon --- * A rect --- Use the POLYGON:FindOnMap() of POLYGON:Find() functions for this. You can also create a non existing polygon in memory using the POLYGON:New() function. Pass in a --- any number of Vec2s into this function to define the shape of the polygon you want. - --- You can draw very intricate and complex polygons in the Mission Editor to avoid (or include) map objects. You can then generate random points within this complex --- shape for spawning groups or checking positions. - --- When a POLYGON is made, it's automatically triangulated. The resulting triangles are stored in POLYGON.Triangles. This also immeadiately saves the surface area --- of the POLYGON. Because the POLYGON is triangulated, it's possible to generate random points within this POLYGON without having to use a trial and error method to see if --- the point is contained within the shape. --- Using POLYGON:GetRandomVec2() will result in a truly, non-biased, random Vec2 within the shape. You'll want to use this function most. There's also POLYGON:GetRandomNonWeightedVec2 --- which ignores the size of the triangles in the polygon to pick a random points. This will result in more points clumping together in parts of the polygon where the triangles are --- the smallest. - - --- @field #POLYGON - -POLYGON = { - ClassName = "POLYGON", - Points = {}, - Coords = {}, - Triangles = {}, - SurfaceArea = 0, - TriangleMarkIDs = {}, - OutlineMarkIDs = {}, - Angle = nil, -- for arrows - Heading = nil -- for arrows -} - ---- Finds a polygon on the map by its name. The polygon must be added in the mission editor. --- @param #string shape_name Name of the polygon to find --- @return #POLYGON The found polygon, or nil if not found -function POLYGON:FindOnMap(shape_name) - local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) - - for _, layer in pairs(env.mission.drawings.layers) do - for _, object in pairs(layer["objects"]) do - if object["name"] == shape_name then - if (object["primitiveType"] == "Line" and object["closed"] == true) or (object["polygonMode"] == "free") then - for _, point in UTILS.spairs(object["points"]) do - local p = {x = object["mapX"] + point["x"], - y = object["mapY"] + point["y"] } - local coord = COORDINATE:NewFromVec2(p) - self.Points[#self.Points + 1] = p - self.Coords[#self.Coords + 1] = coord - end - elseif object["polygonMode"] == "rect" then - local angle = object["angle"] - local half_width = object["width"] / 2 - local half_height = object["height"] / 2 - - local p1 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) - local p2 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) - local p3 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) - local p4 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) - - self.Points = {p1, p2, p3, p4} - for _, point in pairs(self.Points) do - self.Coords[#self.Coords + 1] = COORDINATE:NewFromVec2(point) - end - elseif object["polygonMode"] == "arrow" then - for _, point in UTILS.spairs(object["points"]) do - local p = {x = object["mapX"] + point["x"], - y = object["mapY"] + point["y"] } - local coord = COORDINATE:NewFromVec2(p) - self.Points[#self.Points + 1] = p - self.Coords[#self.Coords + 1] = coord - end - self.Angle = object["angle"] - self.Heading = UTILS.ClampAngle(self.Angle + 90) - end - end - end - end - - if #self.Points == 0 then - return nil - end - - self.CenterVec2 = self:GetCentroid() - self.Triangles = self:Triangulate() - self.SurfaceArea = self:__CalculateSurfaceArea() - - self.TriangleMarkIDs = {} - self.OutlineMarkIDs = {} - return self -end - ---- Creates a polygon from a zone. The zone must be defined in the mission. --- @param #string zone_name Name of the zone --- @return #POLYGON The polygon created from the zone, or nil if the zone is not found -function POLYGON:FromZone(zone_name) - for _, zone in pairs(env.mission.triggers.zones) do - if zone["name"] == zone_name then - return POLYGON:New(unpack(zone["verticies"] or {})) - end - end -end - ---- Finds a polygon by its name in the database. --- @param #string shape_name Name of the polygon to find --- @return #POLYGON The found polygon, or nil if not found -function POLYGON:Find(shape_name) - return _DATABASE:FindShape(shape_name) -end - ---- Creates a new polygon from a list of points. Each point is a table with 'x' and 'y' fields. --- @param #table ... Points of the polygon --- @return #POLYGON The new polygon -function POLYGON:New(...) - local self = BASE:Inherit(self, SHAPE_BASE:New()) - - self.Points = {...} - self.Coords = {} - for _, point in UTILS.spairs(self.Points) do - table.insert(self.Coords, COORDINATE:NewFromVec2(point)) - end - self.Triangles = self:Triangulate() - self.SurfaceArea = self:__CalculateSurfaceArea() - - return self -end - ---- Calculates the centroid of the polygon. The centroid is the average of the 'x' and 'y' coordinates of the points. --- @return #table The centroid of the polygon -function POLYGON:GetCentroid() - local function sum(t) - local total = 0 - for _, value in pairs(t) do - total = total + value - end - return total - end - - local x_values = {} - local y_values = {} - local length = table.length(self.Points) - - for _, point in pairs(self.Points) do - table.insert(x_values, point.x) - table.insert(y_values, point.y) - end - - local x = sum(x_values) / length - local y = sum(y_values) / length - - return { - ["x"] = x, - ["y"] = y - } -end - ---- Returns the coordinates of the polygon. Each coordinate is a COORDINATE object. --- @return #table The coordinates of the polygon -function POLYGON:GetCoordinates() - return self.Coords -end - ---- Returns the start coordinate of the polygon. The start coordinate is the first point of the polygon. --- @return #COORDINATE The start coordinate of the polygon -function POLYGON:GetStartCoordinate() - return self.Coords[1] -end - ---- Returns the end coordinate of the polygon. The end coordinate is the last point of the polygon. --- @return #COORDINATE The end coordinate of the polygon -function POLYGON:GetEndCoordinate() - return self.Coords[#self.Coords] -end - ---- Returns the start point of the polygon. The start point is the first point of the polygon. --- @return #table The start point of the polygon -function POLYGON:GetStartPoint() - return self.Points[1] -end - ---- Returns the end point of the polygon. The end point is the last point of the polygon. --- @return #table The end point of the polygon -function POLYGON:GetEndPoint() - return self.Points[#self.Points] -end - ---- Returns the points of the polygon. Each point is a table with 'x' and 'y' fields. --- @return #table The points of the polygon -function POLYGON:GetPoints() - return self.Points -end - ---- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. --- @return #number The surface area of the polygon -function POLYGON:GetSurfaceArea() - return self.SurfaceArea -end - ---- Calculates the bounding box of the polygon. The bounding box is the smallest rectangle that contains the polygon. --- @return #table The bounding box of the polygon -function POLYGON:GetBoundingBox() - local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[1].x, self.Points[1].y - - for i = 2, #self.Points do - local x, y = self.Points[i].x, self.Points[i].y - - if x < min_x then - min_x = x - end - if y < min_y then - min_y = y - end - if x > max_x then - max_x = x - end - if y > max_y then - max_y = y - end - end - return { - {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} - } -end - ---- Triangulates the polygon. The polygon is divided into triangles. --- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it --- @return #table The triangles of the polygon -function POLYGON:Triangulate(points) - points = points or self.Points - local triangles = {} - - local function get_orientation(shape_points) - local sum = 0 - for i = 1, #shape_points do - local j = i % #shape_points + 1 - sum = sum + (shape_points[j].x - shape_points[i].x) * (shape_points[j].y + shape_points[i].y) - end - return sum >= 0 and "clockwise" or "counter-clockwise" -- sum >= 0, return "clockwise", else return "counter-clockwise" - end - - local function ensure_clockwise(shape_points) - local orientation = get_orientation(shape_points) - if orientation == "counter-clockwise" then - -- Reverse the order of shape_points so they're clockwise - local reversed = {} - for i = #shape_points, 1, -1 do - table.insert(reversed, shape_points[i]) - end - return reversed - end - return shape_points - end - - local function is_clockwise(p1, p2, p3) - local cross_product = (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x) - return cross_product < 0 - end - - local function divide_recursively(shape_points) - if #shape_points == 3 then - table.insert(triangles, TRIANGLE:New(shape_points[1], shape_points[2], shape_points[3])) - elseif #shape_points > 3 then -- find an ear -> a triangle with no other points inside it - for i, p1 in ipairs(shape_points) do - local p2 = shape_points[(i % #shape_points) + 1] - local p3 = shape_points[(i + 1) % #shape_points + 1] - local triangle = TRIANGLE:New(p1, p2, p3) - local is_ear = true - - if not is_clockwise(p1, p2, p3) then - is_ear = false - else - for _, point in ipairs(shape_points) do - if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then - is_ear = false - break - end - end - end - - if is_ear then - -- Check if any point in the original polygon is inside the ear triangle - local is_valid_triangle = true - for _, point in ipairs(points) do - if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then - is_valid_triangle = false - break - end - end - if is_valid_triangle then - table.insert(triangles, triangle) - local remaining_points = {} - for j, point in ipairs(shape_points) do - if point ~= p2 then - table.insert(remaining_points, point) - end - end - divide_recursively(remaining_points) - break - end - end - end - end - end - - points = ensure_clockwise(points) - divide_recursively(points) - return triangles -end - -function POLYGON:CovarianceMatrix() - local cx, cy = self:GetCentroid() - local covXX, covYY, covXY = 0, 0, 0 - for _, p in ipairs(self.points) do - covXX = covXX + (p.x - cx)^2 - covYY = covYY + (p.y - cy)^2 - covXY = covXY + (p.x - cx) * (p.y - cy) - end - covXX = covXX / (#self.points - 1) - covYY = covYY / (#self.points - 1) - covXY = covXY / (#self.points - 1) - return covXX, covYY, covXY -end - -function POLYGON:Direction() - local covXX, covYY, covXY = self:CovarianceMatrix() - -- Simplified calculation for the largest eigenvector's direction - local theta = 0.5 * math.atan2(2 * covXY, covXX - covYY) - return math.cos(theta), math.sin(theta) -end - ---- Returns a random Vec2 within the polygon. The Vec2 is weighted by the areas of the triangles that make up the polygon. --- @return #table The random Vec2 -function POLYGON:GetRandomVec2() - local weights = {} - for _, triangle in pairs(self.Triangles) do - weights[triangle] = triangle.SurfaceArea / self.SurfaceArea - end - - local random_weight = math.random() - local accumulated_weight = 0 - for triangle, weight in pairs(weights) do - accumulated_weight = accumulated_weight + weight - if accumulated_weight >= random_weight then - return triangle:GetRandomVec2() - end - end -end - ---- Returns a random non-weighted Vec2 within the polygon. The Vec2 is chosen from one of the triangles that make up the polygon. --- @return #table The random non-weighted Vec2 -function POLYGON:GetRandomNonWeightedVec2() - return self.Triangles[math.random(1, #self.Triangles)]:GetRandomVec2() -end - ---- Checks if a point is contained within the polygon. The point is a table with 'x' and 'y' fields. --- @param #table point The point to check --- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it --- @return #bool True if the point is contained, false otherwise -function POLYGON:ContainsPoint(point, polygon_points) - local x = point.x - local y = point.y - - polygon_points = polygon_points or self.Points - - local counter = 0 - local num_points = #polygon_points - for current_index = 1, num_points do - local next_index = (current_index % num_points) + 1 - local current_x, current_y = polygon_points[current_index].x, polygon_points[current_index].y - local next_x, next_y = polygon_points[next_index].x, polygon_points[next_index].y - if ((current_y > y) ~= (next_y > y)) and (x < (next_x - current_x) * (y - current_y) / (next_y - current_y) + current_x) then - counter = counter + 1 - end - end - return counter % 2 == 1 -end - ---- Draws the polygon on the map. The polygon can be drawn with or without inner triangles. This is just for debugging --- @param #bool include_inner_triangles Whether to include inner triangles in the drawing -function POLYGON:Draw(include_inner_triangles) - include_inner_triangles = include_inner_triangles or false - for i=1, #self.Coords do - local c1 = self.Coords[i] - local c2 = self.Coords[i % #self.Coords + 1] - table.add(self.OutlineMarkIDs, c1:LineToAll(c2)) - end - - - if include_inner_triangles then - for _, triangle in ipairs(self.Triangles) do - triangle:Draw() - end - end -end - ---- Removes the drawing of the polygon from the map. -function POLYGON:RemoveDraw() - for _, triangle in pairs(self.Triangles) do - triangle:RemoveDraw() - end - for _, mark_id in pairs(self.OutlineMarkIDs) do - UTILS.RemoveMark(mark_id) - end -end - ---- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. --- @return #number The surface area of the polygon -function POLYGON:__CalculateSurfaceArea() - local area = 0 - for _, triangle in pairs(self.Triangles) do - area = area + triangle.SurfaceArea - end - return area -end - - - - - - - - - - - - - - - - - - - - - - - diff --git a/Moose Development/Moose/Shapes/ShapeBase.lua b/Moose Development/Moose/Shapes/ShapeBase.lua deleted file mode 100644 index 7042a44ad..000000000 --- a/Moose Development/Moose/Shapes/ShapeBase.lua +++ /dev/null @@ -1,216 +0,0 @@ ---- **Shapes** - Class that serves as the base shapes drawn in the Mission Editor --- --- --- ### Author: **nielsvaes/coconutcockpit** --- --- === --- @module Shapes.SHAPE_BASE --- @image CORE_Pathline.png - - ---- SHAPE_BASE class. --- @type SHAPE_BASE --- @field #string ClassName Name of the class. --- @field #string Name Name of the shape --- @field #table CenterVec2 Vec2 of the center of the shape, this will be assigned automatically --- @field #table Points List of 3D points defining the shape, this will be assigned automatically --- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically --- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically --- @extends Core.Base#BASE - ---- *I'm in love with the shape of you -- Ed Sheeran --- --- === --- --- # SHAPE_BASE --- The class serves as the base class to deal with these shapes using MOOSE. You should never use this class on its own, --- rather use: --- CIRCLE --- LINE --- OVAL --- POLYGON --- TRIANGLE (although this one's a bit special as well) - --- === --- The idea is that anything you draw on the map in the Mission Editor can be turned in a shape to work with in MOOSE. --- This is the base class that all other shape classes are built on. There are some shared functions, most of which are overridden in the derived classes - --- @field #SHAPE_BASE - - -SHAPE_BASE = { - ClassName = "SHAPE_BASE", - Name = "", - CenterVec2 = nil, - Points = {}, - Coords = {}, - MarkIDs = {}, - ColorString = "", - ColorRGBA = {} -} - ---- Creates a new instance of SHAPE_BASE. --- @return #SHAPE_BASE The new instance -function SHAPE_BASE:New() - local self = BASE:Inherit(self, BASE:New()) - return self -end - ---- Finds a shape on the map by its name. --- @param #string shape_name Name of the shape to find --- @return #SHAPE_BASE The found shape -function SHAPE_BASE:FindOnMap(shape_name) - local self = BASE:Inherit(self, BASE:New()) - - local found = false - - for _, layer in pairs(env.mission.drawings.layers) do - for _, object in pairs(layer["objects"]) do - if object["name"] == shape_name then - self.Name = object["name"] - self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } - self.ColorString = object["colorString"] - self.ColorRGBA = UTILS.HexToRGBA(self.ColorString) - found = true - end - end - end - if not found then - self:E("Can't find a shape with name " .. shape_name) - end - return self -end - -function SHAPE_BASE:GetAllShapes(filter) - filter = filter or "" - local return_shapes = {} - for _, layer in pairs(env.mission.drawings.layers) do - for _, object in pairs(layer["objects"]) do - if string.contains(object["name"], filter) then - table.add(return_shapes, object) - end - end - end - - return return_shapes -end - ---- Offsets the shape to a new position. --- @param #table new_vec2 The new position -function SHAPE_BASE:Offset(new_vec2) - local offset_vec2 = UTILS.Vec2Subtract(new_vec2, self.CenterVec2) - self.CenterVec2 = new_vec2 - if self.ClassName == "POLYGON" then - for _, point in pairs(self.Points) do - point.x = point.x + offset_vec2.x - point.y = point.y + offset_vec2.y - end - end -end - ---- Gets the name of the shape. --- @return #string The name of the shape -function SHAPE_BASE:GetName() - return self.Name -end - -function SHAPE_BASE:GetColorString() - return self.ColorString -end - -function SHAPE_BASE:GetColorRGBA() - return self.ColorRGBA -end - -function SHAPE_BASE:GetColorRed() - return self.ColorRGBA.R -end - -function SHAPE_BASE:GetColorGreen() - return self.ColorRGBA.G -end - -function SHAPE_BASE:GetColorBlue() - return self.ColorRGBA.B -end - -function SHAPE_BASE:GetColorAlpha() - return self.ColorRGBA.A -end - ---- Gets the center position of the shape. --- @return #table The center position -function SHAPE_BASE:GetCenterVec2() - return self.CenterVec2 -end - ---- Gets the center coordinate of the shape. --- @return #COORDINATE The center coordinate -function SHAPE_BASE:GetCenterCoordinate() - return COORDINATE:NewFromVec2(self.CenterVec2) -end - ---- Gets the coordinate of the shape. --- @return #COORDINATE The coordinate -function SHAPE_BASE:GetCoordinate() - return self:GetCenterCoordinate() -end - ---- Checks if a point is contained within the shape. --- @param #table _ The point to check --- @return #bool True if the point is contained, false otherwise -function SHAPE_BASE:ContainsPoint(_) - self:E("This needs to be set in the derived class") -end - ---- Checks if a unit is contained within the shape. --- @param #string unit_name The name of the unit to check --- @return #bool True if the unit is contained, false otherwise -function SHAPE_BASE:ContainsUnit(unit_name) - local unit = UNIT:FindByName(unit_name) - - if unit == nil or not unit:IsAlive() then - return false - end - - if self:ContainsPoint(unit:GetVec2()) then - return true - end - return false -end - ---- Checks if any unit of a group is contained within the shape. --- @param #string group_name The name of the group to check --- @return #bool True if any unit of the group is contained, false otherwise -function SHAPE_BASE:ContainsAnyOfGroup(group_name) - local group = GROUP:FindByName(group_name) - - if group == nil or not group:IsAlive() then - return false - end - - for _, unit in pairs(group:GetUnits()) do - if self:ContainsPoint(unit:GetVec2()) then - return true - end - end - return false -end - ---- Checks if all units of a group are contained within the shape. --- @param #string group_name The name of the group to check --- @return #bool True if all units of the group are contained, false otherwise -function SHAPE_BASE:ContainsAllOfGroup(group_name) - local group = GROUP:FindByName(group_name) - - if group == nil or not group:IsAlive() then - return false - end - - for _, unit in pairs(group:GetUnits()) do - if not self:ContainsPoint(unit:GetVec2()) then - return false - end - end - return true -end diff --git a/Moose Development/Moose/Shapes/Triangle.lua b/Moose Development/Moose/Shapes/Triangle.lua deleted file mode 100644 index c60b2aeef..000000000 --- a/Moose Development/Moose/Shapes/Triangle.lua +++ /dev/null @@ -1,86 +0,0 @@ --- TRIANGLE class with properties and methods for handling triangles. This class is mostly used by the POLYGON class, but you can use it on its own as well --- --- ### Author: **nielsvaes/coconutcockpit** --- --- -TRIANGLE = { - ClassName = "TRIANGLE", - Points = {}, - Coords = {}, - SurfaceArea = 0 -} - ---- Creates a new triangle from three points. The points need to be given as Vec2s --- @param #table p1 The first point of the triangle --- @param #table p2 The second point of the triangle --- @param #table p3 The third point of the triangle --- @return #TRIANGLE The new triangle -function TRIANGLE:New(p1, p2, p3) - local self = BASE:Inherit(self, SHAPE_BASE:New()) - self.Points = {p1, p2, p3} - - local center_x = (p1.x + p2.x + p3.x) / 3 - local center_y = (p1.y + p2.y + p3.y) / 3 - self.CenterVec2 = {x=center_x, y=center_y} - - for _, pt in pairs({p1, p2, p3}) do - table.add(self.Coords, COORDINATE:NewFromVec2(pt)) - end - - self.SurfaceArea = math.abs((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) * 0.5 - - self.MarkIDs = {} - return self -end - ---- Checks if a point is contained within the triangle. --- @param #table pt The point to check --- @param #table points (optional) The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it --- @return #bool True if the point is contained, false otherwise -function TRIANGLE:ContainsPoint(pt, points) - points = points or self.Points - - local function sign(p1, p2, p3) - return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y) - end - - local d1 = sign(pt, self.Points[1], self.Points[2]) - local d2 = sign(pt, self.Points[2], self.Points[3]) - local d3 = sign(pt, self.Points[3], self.Points[1]) - - local has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0) - local has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0) - - return not (has_neg and has_pos) -end - ---- Returns a random Vec2 within the triangle. --- @param #table points The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it --- @return #table The random Vec2 -function TRIANGLE:GetRandomVec2(points) - points = points or self.Points - local pt = {math.random(), math.random()} - table.sort(pt) - local s = pt[1] - local t = pt[2] - pt[1] - local u = 1 - pt[2] - - return {x = s * points[1].x + t * points[2].x + u * points[3].x, - y = s * points[1].y + t * points[2].y + u * points[3].y} -end - ---- Draws the triangle on the map, just for debugging -function TRIANGLE:Draw() - for i=1, #self.Coords do - local c1 = self.Coords[i] - local c2 = self.Coords[i % #self.Coords + 1] - table.add(self.MarkIDs, c1:LineToAll(c2)) - end -end - ---- Removes the drawing of the triangle from the map. -function TRIANGLE:RemoveDraw() - for _, mark_id in pairs(self.MarkIDs) do - UTILS.RemoveMark(mark_id) - end -end diff --git a/Moose Development/Moose/Utilities/Utils.lua b/Moose Development/Moose/Utilities/Utils.lua index 96cf8c1b4..6bf7e1fc9 100644 --- a/Moose Development/Moose/Utilities/Utils.lua +++ b/Moose Development/Moose/Utilities/Utils.lua @@ -3513,25 +3513,6 @@ function string.contains(str, value) return string.match(str, value) end - ---- Moves an object from one table to another --- @param #obj object to move --- @param #from_table table to move from --- @param #to_table table to move to -function table.move_object(obj, from_table, to_table) - local index - for i, v in pairs(from_table) do - if v == obj then - index = i - end - end - - if index then - local moved = table.remove(from_table, index) - table.insert_unique(to_table, moved) - end -end - --- Given tbl is a indexed table ({"hello", "dcs", "world"}), checks if element exists in the table. --- The table can be made up out of complex tables or values as well -- @param #table tbl @@ -3750,25 +3731,6 @@ function UTILS.OctalToDecimal(Number) return tonumber(Number,8) end - ---- HexToRGBA --- @param hex_string table --- @return #table R, G, B, A -function UTILS.HexToRGBA(hex_string) - local hexNumber = tonumber(string.sub(hex_string, 3), 16) -- convert the string to a number - -- extract RGBA components - local alpha = hexNumber % 256 - hexNumber = (hexNumber - alpha) / 256 - local blue = hexNumber % 256 - hexNumber = (hexNumber - blue) / 256 - local green = hexNumber % 256 - hexNumber = (hexNumber - green) / 256 - local red = hexNumber % 256 - - return {R = red, G = green, B = blue, A = alpha} -end - - --- Function to save the position of a set of #OPSGROUP (ARMYGROUP) objects. -- @param Core.Set#SET_OPSGROUP Set of ops objects to save -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. @@ -3806,7 +3768,7 @@ function UTILS.SaveSetOfOpsGroups(Set,Path,Filename,Structured) data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d,%s\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z,strucdata) else data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z) - end + end end end -- save the data @@ -3818,12 +3780,12 @@ end -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. -- @param #string Filename The name of the file. -- @return #table Returns a table of data entries: `{ groupname=groupname, size=size, coordinate=coordinate, template=template, structure=structure, legion=legion, alttemplate=alttemplate }` --- Returns nil when the file cannot be read. +-- Returns nil when the file cannot be read. function UTILS.LoadSetOfOpsGroups(Path,Filename) local filename = Filename or "SetOfGroups" local datatable = {} - + if UTILS.CheckFileExists(Path,filename) then local outcome,loadeddata = UTILS.LoadFromFile(Path,Filename) -- remove header @@ -3858,20 +3820,20 @@ end -- @param #number tgtHdg The absolute heading from the reference object to the target object/point in 0-360 -- @return #string text Text in clock heading such as "4 O'CLOCK" -- @usage Display the range and clock distance of a BTR in relation to REAPER 1-1's heading: --- +-- -- myUnit = UNIT:FindByName( "REAPER 1-1" ) -- myTarget = GROUP:FindByName( "BTR-1" ) --- +-- -- coordUnit = myUnit:GetCoordinate() -- coordTarget = myTarget:GetCoordinate() --- +-- -- hdgUnit = myUnit:GetHeading() -- hdgTarget = coordUnit:HeadingTo( coordTarget ) -- distTarget = coordUnit:Get3DDistance( coordTarget ) --- +-- -- clockString = UTILS.ClockHeadingString( hdgUnit, hdgTarget ) --- --- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! +-- +-- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! -- MESSAGE:New("Contact BTR at " .. clockString .. " for " .. distTarget .. "m!):ToUnit( myUnit ) function UTILS.ClockHeadingString(refHdg,tgtHdg) local relativeAngle = tgtHdg - refHdg From 3d7172fdf770b74e61288dacdef4201d7a6f0012 Mon Sep 17 00:00:00 2001 From: Niels Vaes Date: Tue, 23 Apr 2024 09:13:52 +0200 Subject: [PATCH 12/15] Adding SHAPES (#2113) * Adding a new TerminalType (100)that seems to be introduced in the update that brought Muwaffaq Salti. The base has a couple of spots (like 04, 05, 06) that can only accommodate smaller type fixed wing aircraft, like the F-16, but not bigger types like the Warthog of the Strike Eagle. Because we weren't checking for this new type, spawning in these particular spots always resulted in an airstart, because `_CheckTerminalType` would always return `false` * Adding Shapes over from old MOOSE branch * cleanup * adding HEXtoRGBA * removing Arrow.lua, it's part of Polygon.lua --- Moose Development/Moose/Modules.lua | 8 + Moose Development/Moose/Shapes/Circle.lua | 259 +++++++++++ Moose Development/Moose/Shapes/Cube.lua | 66 +++ Moose Development/Moose/Shapes/Line.lua | 331 ++++++++++++++ Moose Development/Moose/Shapes/Oval.lua | 213 +++++++++ Moose Development/Moose/Shapes/Polygon.lua | 458 +++++++++++++++++++ Moose Development/Moose/Shapes/ShapeBase.lua | 216 +++++++++ Moose Development/Moose/Shapes/Triangle.lua | 86 ++++ Moose Development/Moose/Utilities/Utils.lua | 56 ++- 9 files changed, 1684 insertions(+), 9 deletions(-) create mode 100644 Moose Development/Moose/Shapes/Circle.lua create mode 100644 Moose Development/Moose/Shapes/Cube.lua create mode 100644 Moose Development/Moose/Shapes/Line.lua create mode 100644 Moose Development/Moose/Shapes/Oval.lua create mode 100644 Moose Development/Moose/Shapes/Polygon.lua create mode 100644 Moose Development/Moose/Shapes/ShapeBase.lua create mode 100644 Moose Development/Moose/Shapes/Triangle.lua diff --git a/Moose Development/Moose/Modules.lua b/Moose Development/Moose/Modules.lua index c483b8d21..73cdbeb37 100644 --- a/Moose Development/Moose/Modules.lua +++ b/Moose Development/Moose/Modules.lua @@ -122,6 +122,14 @@ __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Route.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Account.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Assist.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/ShapeBase.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Circle.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Cube.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Line.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Oval.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Polygon.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Triangle.lua' ) + __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/UserSound.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/SoundOutput.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/Radio.lua' ) diff --git a/Moose Development/Moose/Shapes/Circle.lua b/Moose Development/Moose/Shapes/Circle.lua new file mode 100644 index 000000000..04c153d86 --- /dev/null +++ b/Moose Development/Moose/Shapes/Circle.lua @@ -0,0 +1,259 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.CIRCLE + +--- CIRCLE class. +-- @type CIRCLE +-- @field #string ClassName Name of the class. +-- @field #number Radius Radius of the circle + +--- *It's NOT hip to be square* -- Someone, somewhere, probably +-- +-- === +-- +-- # CIRCLE +-- CIRCLEs can be fetched from the drawings in the Mission Editor + +-- This class has some of the standard CIRCLE functions you'd expect. One function of interest is CIRCLE:PointInSector() that you can use if a point is +-- within a certain sector (pizza slice) of a circle. This can be useful for many things, including rudimentary, "radar-like" searches from a unit. + +-- @field #CIRCLE + +--- CIRCLE class with properties and methods for handling circles. +CIRCLE = { + ClassName = "CIRCLE", + Radius = nil, +} +--- Finds a circle on the map by its name. The circle must have been added in the Mission Editor +-- @param #string shape_name Name of the circle to find +-- @return #CIRCLE The found circle, or nil if not found +function CIRCLE:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.find(object["name"], shape_name, 1, true) then + if object["polygonMode"] == "circle" then + self.Radius = object["radius"] + end + end + end + end + + return self +end + +--- Finds a circle by its name in the database. +-- @param #string shape_name Name of the circle to find +-- @return #CIRCLE The found circle, or nil if not found +function CIRCLE:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new circle from a center point and a radius. +-- @param #table vec2 The center point of the circle +-- @param #number radius The radius of the circle +-- @return #CIRCLE The new circle +function CIRCLE:New(vec2, radius) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = vec2 + self.Radius = radius + return self +end + +--- Gets the radius of the circle. +-- @return #number The radius of the circle +function CIRCLE:GetRadius() + return self.Radius +end + +--- Checks if a point is contained within the circle. +-- @param #table point The point to check +-- @return #bool True if the point is contained, false otherwise +function CIRCLE:ContainsPoint(point) + if ((point.x - self.CenterVec2.x) ^ 2 + (point.y - self.CenterVec2.y) ^ 2) ^ 0.5 <= self.Radius then + return true + end + return false +end + +--- Checks if a point is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #table point The point to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if the point is contained, false otherwise +function CIRCLE:PointInSector(point, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + local function are_clockwise(v1, v2) + return -v1.x * v2.y + v1.y * v2.x > 0 + end + + local function is_in_radius(rp) + return rp.x * rp.x + rp.y * rp.y <= radius ^ 2 + end + + local rel_pt = { + x = point.x - center.x, + y = point.y - center.y + } + + local rel_sector_start = { + x = sector_start.x - center.x, + y = sector_start.y - center.y, + } + + local rel_sector_end = { + x = sector_end.x - center.x, + y = sector_end.y - center.y, + } + + return not are_clockwise(rel_sector_start, rel_pt) and + are_clockwise(rel_sector_end, rel_pt) and + is_in_radius(rel_pt, radius) +end + +--- Checks if a unit is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string unit_name The name of the unit to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if the unit is contained, false otherwise +function CIRCLE:UnitInSector(unit_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + if self:PointInSector(UNIT:FindByName(unit_name):GetVec2(), sector_start, sector_end, center, radius) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string group_name The name of the group to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if any unit of the group is contained, false otherwise +function CIRCLE:AnyOfGroupInSector(group_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string group_name The name of the group to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if all units of the group are contained, false otherwise +function CIRCLE:AllOfGroupInSector(group_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if not self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then + return false + end + end + return true +end + +--- Checks if a unit is contained within a radius of the circle. +-- @param #string unit_name The name of the unit to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if the unit is contained, false otherwise +function CIRCLE:UnitInRadius(unit_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + if UTILS.IsInRadius(center, UNIT:FindByName(unit_name):GetVec2(), radius) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within a radius of the circle. +-- @param #string group_name The name of the group to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if any unit of the group is contained, false otherwise +function CIRCLE:AnyOfGroupInRadius(group_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if UTILS.IsInRadius(center, unit:GetVec2(), radius) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within a radius of the circle. +-- @param #string group_name The name of the group to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if all units of the group are contained, false otherwise +function CIRCLE:AllOfGroupInRadius(group_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if not UTILS.IsInRadius(center, unit:GetVec2(), radius) then + return false + end + end + return true +end + +--- Returns a random Vec2 within the circle. +-- @return #table The random Vec2 +function CIRCLE:GetRandomVec2() + local angle = math.random() * 2 * math.pi + + local rx = math.random(0, self.Radius) * math.cos(angle) + self.CenterVec2.x + local ry = math.random(0, self.Radius) * math.sin(angle) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Returns a random Vec2 on the border of the circle. +-- @return #table The random Vec2 +function CIRCLE:GetRandomVec2OnBorder() + local angle = math.random() * 2 * math.pi + + local rx = self.Radius * math.cos(angle) + self.CenterVec2.x + local ry = self.Radius * math.sin(angle) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Calculates the bounding box of the circle. The bounding box is the smallest rectangle that contains the circle. +-- @return #table The bounding box of the circle +function CIRCLE:GetBoundingBox() + local min_x = self.CenterVec2.x - self.Radius + local min_y = self.CenterVec2.y - self.Radius + local max_x = self.CenterVec2.x + self.Radius + local max_y = self.CenterVec2.y + self.Radius + + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + diff --git a/Moose Development/Moose/Shapes/Cube.lua b/Moose Development/Moose/Shapes/Cube.lua new file mode 100644 index 000000000..ae3f73090 --- /dev/null +++ b/Moose Development/Moose/Shapes/Cube.lua @@ -0,0 +1,66 @@ +CUBE = { + ClassName = "CUBE", + Points = {}, + Coords = {} +} + +--- Points need to be added in the following order: +--- p1 -> p4 make up the front face of the cube +--- p5 -> p8 make up the back face of the cube +--- p1 connects to p5 +--- p2 connects to p6 +--- p3 connects to p7 +--- p4 connects to p8 +--- +--- 8-----------7 +--- /| /| +--- / | / | +--- 4--+--------3 | +--- | | | | +--- | | | | +--- | | | | +--- | 5--------+--6 +--- | / | / +--- |/ |/ +--- 1-----------2 +--- +function CUBE:New(p1, p2, p3, p4, p5, p6, p7, p8) + local self = BASE:Inherit(self, SHAPE_BASE) + self.Points = {p1, p2, p3, p4, p5, p6, p7, p8} + for _, point in spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec3(point)) + end + return self +end + +function CUBE:GetCenter() + local center = { x=0, y=0, z=0 } + for _, point in pairs(self.Points) do + center.x = center.x + point.x + center.y = center.y + point.y + center.z = center.z + point.z + end + + center.x = center.x / 8 + center.y = center.y / 8 + center.z = center.z / 8 + return center +end + +function CUBE:ContainsPoint(point, cube_points) + cube_points = cube_points or self.Points + local min_x, min_y, min_z = math.huge, math.huge, math.huge + local max_x, max_y, max_z = -math.huge, -math.huge, -math.huge + + -- Find the minimum and maximum x, y, and z values of the cube points + for _, p in ipairs(cube_points) do + if p.x < min_x then min_x = p.x end + if p.y < min_y then min_y = p.y end + if p.z < min_z then min_z = p.z end + if p.x > max_x then max_x = p.x end + if p.y > max_y then max_y = p.y end + if p.z > max_z then max_z = p.z end + end + + return point.x >= min_x and point.x <= max_x and point.y >= min_y and point.y <= max_y and point.z >= min_z and point.z <= max_z +end diff --git a/Moose Development/Moose/Shapes/Line.lua b/Moose Development/Moose/Shapes/Line.lua new file mode 100644 index 000000000..08f7c84a0 --- /dev/null +++ b/Moose Development/Moose/Shapes/Line.lua @@ -0,0 +1,331 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.LINE + +--- OVAL class. +-- @type OVAL +-- @field #string ClassName Name of the class. +-- @field #number Points points of the line +-- @field #number Coords coordinates of the line + +-- +-- === + +-- @field #LINE +LINE = { + ClassName = "LINE", + Points = {}, + Coords = {}, +} + +--- Finds a line on the map by its name. The line must be drawn in the Mission Editor +-- @param #string line_name Name of the line to find +-- @return #LINE The found line, or nil if not found +function LINE:FindOnMap(line_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(line_name)) + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == line_name then + if object["primitiveType"] == "Line" then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + table.insert(self.Points, p) + table.insert(self.Coords, coord) + end + end + end + end + end + + self:I(#self.Points) + if #self.Points == 0 then + return nil + end + + self.MarkIDs = {} + + return self +end + +--- Finds a line by its name in the database. +-- @param #string shape_name Name of the line to find +-- @return #LINE The found line, or nil if not found +function LINE:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new line from two points. +-- @param #table vec2 The first point of the line +-- @param #number radius The second point of the line +-- @return #LINE The new line +function LINE:New(...) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.Points = {...} + self:I(self.Points) + for _, point in UTILS.spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + return self +end + +--- Creates a new line from a circle. +-- @param #table center_point center point of the circle +-- @param #number radius radius of the circle, half length of the line +-- @param #number angle_degrees degrees the line will form from center point +-- @return #LINE The new line +function LINE:NewFromCircle(center_point, radius, angle_degrees) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = center_point + local angleRadians = math.rad(angle_degrees) + + local point1 = { + x = center_point.x + radius * math.cos(angleRadians), + y = center_point.y + radius * math.sin(angleRadians) + } + + local point2 = { + x = center_point.x + radius * math.cos(angleRadians + math.pi), + y = center_point.y + radius * math.sin(angleRadians + math.pi) + } + + for _, point in pairs{point1, point2} do + table.insert(self.Points, point) + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + + return self +end + +--- Gets the coordinates of the line. +-- @return #table The coordinates of the line +function LINE:Coordinates() + return self.Coords +end + +--- Gets the start coordinate of the line. The start coordinate is the first point of the line. +-- @return #COORDINATE The start coordinate of the line +function LINE:GetStartCoordinate() + return self.Coords[1] +end + +--- Gets the end coordinate of the line. The end coordinate is the last point of the line. +-- @return #COORDINATE The end coordinate of the line +function LINE:GetEndCoordinate() + return self.Coords[#self.Coords] +end + +--- Gets the start point of the line. The start point is the first point of the line. +-- @return #table The start point of the line +function LINE:GetStartPoint() + return self.Points[1] +end + +--- Gets the end point of the line. The end point is the last point of the line. +-- @return #table The end point of the line +function LINE:GetEndPoint() + return self.Points[#self.Points] +end + +--- Gets the length of the line. +-- @return #number The length of the line +function LINE:GetLength() + local total_length = 0 + for i=1, #self.Points - 1 do + local x1, y1 = self.Points[i]["x"], self.Points[i]["y"] + local x2, y2 = self.Points[i+1]["x"], self.Points[i+1]["y"] + local segment_length = math.sqrt((x2 - x1)^2 + (y2 - y1)^2) + total_length = total_length + segment_length + end + return total_length +end + +--- Returns a random point on the line. +-- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it +-- @return #table The random point +function LINE:GetRandomPoint(points) + points = points or self.Points + local rand = math.random() -- 0->1 + + local random_x = points[1].x + rand * (points[2].x - points[1].x) + local random_y = points[1].y + rand * (points[2].y - points[1].y) + + return { x= random_x, y= random_y } +end + +--- Gets the heading of the line. +-- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it +-- @return #number The heading of the line +function LINE:GetHeading(points) + points = points or self.Points + + local angle = math.atan2(points[2].y - points[1].y, points[2].x - points[1].x) + + angle = math.deg(angle) + if angle < 0 then + angle = angle + 360 + end + + return angle +end + + +--- Return each part of the line as a new line +-- @return #table The points +function LINE:GetIndividualParts() + local parts = {} + if #self.Points == 2 then + parts = {self} + end + + for i=1, #self.Points -1 do + local p1 = self.Points[i] + local p2 = self.Points[i % #self.Points + 1] + table.add(parts, LINE:New(p1, p2)) + end + + return parts +end + +--- Gets a number of points in between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @return #table The points +function LINE:GetPointsInbetween(amount, start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + if amount == 0 then return {start_point, end_point} end + + amount = amount + 1 + local points = {} + + local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } + local divided = { x = difference.x / amount, y = difference.y / amount } + + for j=0, amount do + local part_pos = {x = divided.x * j, y = divided.y * j} + -- add part_pos vector to the start point so the new point is placed along in the line + local point = {x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} + table.insert(points, point) + end + return points +end + +--- Gets a number of points in between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @return #table The points +function LINE:GetCoordinatesInBetween(amount, start_point, end_point) + local coords = {} + for _, pt in pairs(self:GetPointsInbetween(amount, start_point, end_point)) do + table.add(coords, COORDINATE:NewFromVec2(pt)) + end + return coords +end + + +function LINE:GetRandomPoint(start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + + local fraction = math.random() + + local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } + local part_pos = {x = difference.x * fraction, y = difference.y * fraction} + local random_point = { x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} + + return random_point +end + + +function LINE:GetRandomCoordinate(start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + + return COORDINATE:NewFromVec2(self:GetRandomPoint(start_point, end_point)) +end + + +--- Gets a number of points on a sine wave between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @param #number frequency (Optional) The frequency of the sine wave, default 1 +-- @param #number phase (Optional) The phase of the sine wave, default 0 +-- @param #number amplitude (Optional) The amplitude of the sine wave, default 100 +-- @return #table The points +function LINE:GetPointsBetweenAsSineWave(amount, start_point, end_point, frequency, phase, amplitude) + amount = amount or 20 + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + frequency = frequency or 1 -- number of cycles per unit of x + phase = phase or 0 -- offset in radians + amplitude = amplitude or 100 -- maximum height of the wave + + local points = {} + + -- Returns the y-coordinate of the sine wave at x + local function sine_wave(x) + return amplitude * math.sin(2 * math.pi * frequency * (x - start_point.x) + phase) + end + + -- Plot x-amount of points on the sine wave between point_01 and point_02 + local x = start_point.x + local step = (end_point.x - start_point.x) / 20 + for _=1, amount do + local y = sine_wave(x) + x = x + step + table.add(points, {x=x, y=y}) + end + return points +end + +--- Calculates the bounding box of the line. The bounding box is the smallest rectangle that contains the line. +-- @return #table The bounding box of the line +function LINE:GetBoundingBox() + local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[2].x, self.Points[2].y + + for i = 2, #self.Points do + local x, y = self.Points[i].x, self.Points[i].y + + if x < min_x then + min_x = x + end + if y < min_y then + min_y = y + end + if x > max_x then + max_x = x + end + if y > max_y then + max_y = y + end + end + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Draws the line on the map. +-- @param #table points The points of the line +function LINE:Draw() + for i=1, #self.Coords -1 do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.MarkIDs, c1:LineToAll(c2)) + end +end + +--- Removes the drawing of the line from the map. +function LINE:RemoveDraw() + for _, mark_id in pairs(self.MarkIDs) do + UTILS.RemoveMark(mark_id) + end +end diff --git a/Moose Development/Moose/Shapes/Oval.lua b/Moose Development/Moose/Shapes/Oval.lua new file mode 100644 index 000000000..d2f85a822 --- /dev/null +++ b/Moose Development/Moose/Shapes/Oval.lua @@ -0,0 +1,213 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.OVAL + +--- OVAL class. +-- @type OVAL +-- @field #string ClassName Name of the class. +-- @field #number MajorAxis The major axis (radius) of the oval +-- @field #number MinorAxis The minor axis (radius) of the oval +-- @field #number Angle The angle the oval is rotated on + +--- *The little man removed his hat, what an egg shaped head he had* -- Agatha Christie +-- +-- === +-- +-- # OVAL +-- OVALs can be fetched from the drawings in the Mission Editor + +-- The major and minor axes define how elongated the shape of an oval is. This class has some basic functions that the other SHAPE classes have as well. +-- Since it's not possible to draw the shape of an oval while the mission is running, right now the draw function draws 2 cicles. One with the major axis and one with +-- the minor axis. It then draws a diamond shape on an angle where the corners touch the major and minor axes to give an indication of what the oval actually +-- looks like. + +-- Using ovals can be handy to find an area on the ground that is actually an intersection of a cone and a plane. So imagine you're faking the view cone of +-- a targeting pod and + +-- @field #OVAL + +--- OVAL class with properties and methods for handling ovals. +OVAL = { + ClassName = "OVAL", + MajorAxis = nil, + MinorAxis = nil, + Angle = 0, + DrawPoly=nil +} + +--- Finds an oval on the map by its name. The oval must be drawn on the map. +-- @param #string shape_name Name of the oval to find +-- @return #OVAL The found oval, or nil if not found +function OVAL:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.find(object["name"], shape_name, 1, true) then + if object["polygonMode"] == "oval" then + self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } + self.MajorAxis = object["r1"] + self.MinorAxis = object["r2"] + self.Angle = object["angle"] + end + end + end + end + + return self +end + +--- Finds an oval by its name in the database. +-- @param #string shape_name Name of the oval to find +-- @return #OVAL The found oval, or nil if not found +function OVAL:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new oval from a center point, major axis, minor axis, and angle. +-- @param #table vec2 The center point of the oval +-- @param #number major_axis The major axis of the oval +-- @param #number minor_axis The minor axis of the oval +-- @param #number angle The angle of the oval +-- @return #OVAL The new oval +function OVAL:New(vec2, major_axis, minor_axis, angle) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = vec2 + self.MajorAxis = major_axis + self.MinorAxis = minor_axis + self.Angle = angle or 0 + + return self +end + +--- Gets the major axis of the oval. +-- @return #number The major axis of the oval +function OVAL:GetMajorAxis() + return self.MajorAxis +end + +--- Gets the minor axis of the oval. +-- @return #number The minor axis of the oval +function OVAL:GetMinorAxis() + return self.MinorAxis +end + +--- Gets the angle of the oval. +-- @return #number The angle of the oval +function OVAL:GetAngle() + return self.Angle +end + +--- Sets the major axis of the oval. +-- @param #number value The new major axis +function OVAL:SetMajorAxis(value) + self.MajorAxis = value +end + +--- Sets the minor axis of the oval. +-- @param #number value The new minor axis +function OVAL:SetMinorAxis(value) + self.MinorAxis = value +end + +--- Sets the angle of the oval. +-- @param #number value The new angle +function OVAL:SetAngle(value) + self.Angle = value +end + +--- Checks if a point is contained within the oval. +-- @param #table point The point to check +-- @return #bool True if the point is contained, false otherwise +function OVAL:ContainsPoint(point) + local cos, sin = math.cos, math.sin + local dx = point.x - self.CenterVec2.x + local dy = point.y - self.CenterVec2.y + local rx = dx * cos(self.Angle) + dy * sin(self.Angle) + local ry = -dx * sin(self.Angle) + dy * cos(self.Angle) + return rx * rx / (self.MajorAxis * self.MajorAxis) + ry * ry / (self.MinorAxis * self.MinorAxis) <= 1 +end + +--- Returns a random Vec2 within the oval. +-- @return #table The random Vec2 +function OVAL:GetRandomVec2() + local theta = math.rad(self.Angle) + + local random_point = math.sqrt(math.random()) --> uniformly + --local random_point = math.random() --> more clumped around center + local phi = math.random() * 2 * math.pi + local x_c = random_point * math.cos(phi) + local y_c = random_point * math.sin(phi) + local x_e = x_c * self.MajorAxis + local y_e = y_c * self.MinorAxis + local rx = (x_e * math.cos(theta) - y_e * math.sin(theta)) + self.CenterVec2.x + local ry = (x_e * math.sin(theta) + y_e * math.cos(theta)) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Calculates the bounding box of the oval. The bounding box is the smallest rectangle that contains the oval. +-- @return #table The bounding box of the oval +function OVAL:GetBoundingBox() + local min_x = self.CenterVec2.x - self.MajorAxis + local min_y = self.CenterVec2.y - self.MinorAxis + local max_x = self.CenterVec2.x + self.MajorAxis + local max_y = self.CenterVec2.y + self.MinorAxis + + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Draws the oval on the map, for debugging +-- @param #number angle (Optional) The angle of the oval. If nil will use self.Angle +function OVAL:Draw() + --for pt in pairs(self:PointsOnEdge(20)) do + -- COORDINATE:NewFromVec2(pt) + --end + + self.DrawPoly = POLYGON:NewFromPoints(self:PointsOnEdge(20)) + self.DrawPoly:Draw(true) + + + + + ---- TODO: draw a better shape using line segments + --angle = angle or self.Angle + --local coor = self:GetCenterCoordinate() + -- + --table.add(self.MarkIDs, coor:CircleToAll(self.MajorAxis)) + --table.add(self.MarkIDs, coor:CircleToAll(self.MinorAxis)) + --table.add(self.MarkIDs, coor:LineToAll(coor:Translate(self.MajorAxis, self.Angle))) + -- + --local pt_1 = coor:Translate(self.MajorAxis, self.Angle) + --local pt_2 = coor:Translate(self.MinorAxis, self.Angle - 90) + --local pt_3 = coor:Translate(self.MajorAxis, self.Angle - 180) + --local pt_4 = coor:Translate(self.MinorAxis, self.Angle - 270) + --table.add(self.MarkIDs, pt_1:QuadToAll(pt_2, pt_3, pt_4), -1, {0, 1, 0}, 1, {0, 1, 0}) +end + +--- Removes the drawing of the oval from the map +function OVAL:RemoveDraw() + self.DrawPoly:RemoveDraw() +end + + +function OVAL:PointsOnEdge(num_points) + num_points = num_points or 20 + local points = {} + local dtheta = 2 * math.pi / num_points + + for i = 0, num_points - 1 do + local theta = i * dtheta + local x = self.CenterVec2.x + self.MajorAxis * math.cos(theta) * math.cos(self.Angle) - self.MinorAxis * math.sin(theta) * math.sin(self.Angle) + local y = self.CenterVec2.y + self.MajorAxis * math.cos(theta) * math.sin(self.Angle) + self.MinorAxis * math.sin(theta) * math.cos(self.Angle) + table.insert(points, {x = x, y = y}) + end + + return points +end + + diff --git a/Moose Development/Moose/Shapes/Polygon.lua b/Moose Development/Moose/Shapes/Polygon.lua new file mode 100644 index 000000000..a40256ecf --- /dev/null +++ b/Moose Development/Moose/Shapes/Polygon.lua @@ -0,0 +1,458 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.POLYGON + +--- POLYGON class. +-- @type POLYGON +-- @field #string ClassName Name of the class. +-- @field #table Points List of 3D points defining the shape, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table Triangles List of TRIANGLEs that make up the shape of the POLYGON after being triangulated +-- @extends Core.Base#BASE + +--- *Polygons are fashionable at the moment* -- Trip Hawkins +-- +-- === +-- +-- # POLYGON +-- POLYGONs can be fetched from the drawings in the Mission Editor if the drawing is: +-- * A closed shape made with line segments +-- * A closed shape made with a freehand line +-- * A freehand drawn polygon +-- * A rect +-- Use the POLYGON:FindOnMap() of POLYGON:Find() functions for this. You can also create a non existing polygon in memory using the POLYGON:New() function. Pass in a +-- any number of Vec2s into this function to define the shape of the polygon you want. + +-- You can draw very intricate and complex polygons in the Mission Editor to avoid (or include) map objects. You can then generate random points within this complex +-- shape for spawning groups or checking positions. + +-- When a POLYGON is made, it's automatically triangulated. The resulting triangles are stored in POLYGON.Triangles. This also immeadiately saves the surface area +-- of the POLYGON. Because the POLYGON is triangulated, it's possible to generate random points within this POLYGON without having to use a trial and error method to see if +-- the point is contained within the shape. +-- Using POLYGON:GetRandomVec2() will result in a truly, non-biased, random Vec2 within the shape. You'll want to use this function most. There's also POLYGON:GetRandomNonWeightedVec2 +-- which ignores the size of the triangles in the polygon to pick a random points. This will result in more points clumping together in parts of the polygon where the triangles are +-- the smallest. + + +-- @field #POLYGON + +POLYGON = { + ClassName = "POLYGON", + Points = {}, + Coords = {}, + Triangles = {}, + SurfaceArea = 0, + TriangleMarkIDs = {}, + OutlineMarkIDs = {}, + Angle = nil, -- for arrows + Heading = nil -- for arrows +} + +--- Finds a polygon on the map by its name. The polygon must be added in the mission editor. +-- @param #string shape_name Name of the polygon to find +-- @return #POLYGON The found polygon, or nil if not found +function POLYGON:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == shape_name then + if (object["primitiveType"] == "Line" and object["closed"] == true) or (object["polygonMode"] == "free") then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + self.Points[#self.Points + 1] = p + self.Coords[#self.Coords + 1] = coord + end + elseif object["polygonMode"] == "rect" then + local angle = object["angle"] + local half_width = object["width"] / 2 + local half_height = object["height"] / 2 + + local p1 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) + local p2 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) + local p3 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) + local p4 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) + + self.Points = {p1, p2, p3, p4} + for _, point in pairs(self.Points) do + self.Coords[#self.Coords + 1] = COORDINATE:NewFromVec2(point) + end + elseif object["polygonMode"] == "arrow" then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + self.Points[#self.Points + 1] = p + self.Coords[#self.Coords + 1] = coord + end + self.Angle = object["angle"] + self.Heading = UTILS.ClampAngle(self.Angle + 90) + end + end + end + end + + if #self.Points == 0 then + return nil + end + + self.CenterVec2 = self:GetCentroid() + self.Triangles = self:Triangulate() + self.SurfaceArea = self:__CalculateSurfaceArea() + + self.TriangleMarkIDs = {} + self.OutlineMarkIDs = {} + return self +end + +--- Creates a polygon from a zone. The zone must be defined in the mission. +-- @param #string zone_name Name of the zone +-- @return #POLYGON The polygon created from the zone, or nil if the zone is not found +function POLYGON:FromZone(zone_name) + for _, zone in pairs(env.mission.triggers.zones) do + if zone["name"] == zone_name then + return POLYGON:New(unpack(zone["verticies"] or {})) + end + end +end + +--- Finds a polygon by its name in the database. +-- @param #string shape_name Name of the polygon to find +-- @return #POLYGON The found polygon, or nil if not found +function POLYGON:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new polygon from a list of points. Each point is a table with 'x' and 'y' fields. +-- @param #table ... Points of the polygon +-- @return #POLYGON The new polygon +function POLYGON:New(...) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + + self.Points = {...} + self.Coords = {} + for _, point in UTILS.spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + self.Triangles = self:Triangulate() + self.SurfaceArea = self:__CalculateSurfaceArea() + + return self +end + +--- Calculates the centroid of the polygon. The centroid is the average of the 'x' and 'y' coordinates of the points. +-- @return #table The centroid of the polygon +function POLYGON:GetCentroid() + local function sum(t) + local total = 0 + for _, value in pairs(t) do + total = total + value + end + return total + end + + local x_values = {} + local y_values = {} + local length = table.length(self.Points) + + for _, point in pairs(self.Points) do + table.insert(x_values, point.x) + table.insert(y_values, point.y) + end + + local x = sum(x_values) / length + local y = sum(y_values) / length + + return { + ["x"] = x, + ["y"] = y + } +end + +--- Returns the coordinates of the polygon. Each coordinate is a COORDINATE object. +-- @return #table The coordinates of the polygon +function POLYGON:GetCoordinates() + return self.Coords +end + +--- Returns the start coordinate of the polygon. The start coordinate is the first point of the polygon. +-- @return #COORDINATE The start coordinate of the polygon +function POLYGON:GetStartCoordinate() + return self.Coords[1] +end + +--- Returns the end coordinate of the polygon. The end coordinate is the last point of the polygon. +-- @return #COORDINATE The end coordinate of the polygon +function POLYGON:GetEndCoordinate() + return self.Coords[#self.Coords] +end + +--- Returns the start point of the polygon. The start point is the first point of the polygon. +-- @return #table The start point of the polygon +function POLYGON:GetStartPoint() + return self.Points[1] +end + +--- Returns the end point of the polygon. The end point is the last point of the polygon. +-- @return #table The end point of the polygon +function POLYGON:GetEndPoint() + return self.Points[#self.Points] +end + +--- Returns the points of the polygon. Each point is a table with 'x' and 'y' fields. +-- @return #table The points of the polygon +function POLYGON:GetPoints() + return self.Points +end + +--- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. +-- @return #number The surface area of the polygon +function POLYGON:GetSurfaceArea() + return self.SurfaceArea +end + +--- Calculates the bounding box of the polygon. The bounding box is the smallest rectangle that contains the polygon. +-- @return #table The bounding box of the polygon +function POLYGON:GetBoundingBox() + local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[1].x, self.Points[1].y + + for i = 2, #self.Points do + local x, y = self.Points[i].x, self.Points[i].y + + if x < min_x then + min_x = x + end + if y < min_y then + min_y = y + end + if x > max_x then + max_x = x + end + if y > max_y then + max_y = y + end + end + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Triangulates the polygon. The polygon is divided into triangles. +-- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it +-- @return #table The triangles of the polygon +function POLYGON:Triangulate(points) + points = points or self.Points + local triangles = {} + + local function get_orientation(shape_points) + local sum = 0 + for i = 1, #shape_points do + local j = i % #shape_points + 1 + sum = sum + (shape_points[j].x - shape_points[i].x) * (shape_points[j].y + shape_points[i].y) + end + return sum >= 0 and "clockwise" or "counter-clockwise" -- sum >= 0, return "clockwise", else return "counter-clockwise" + end + + local function ensure_clockwise(shape_points) + local orientation = get_orientation(shape_points) + if orientation == "counter-clockwise" then + -- Reverse the order of shape_points so they're clockwise + local reversed = {} + for i = #shape_points, 1, -1 do + table.insert(reversed, shape_points[i]) + end + return reversed + end + return shape_points + end + + local function is_clockwise(p1, p2, p3) + local cross_product = (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x) + return cross_product < 0 + end + + local function divide_recursively(shape_points) + if #shape_points == 3 then + table.insert(triangles, TRIANGLE:New(shape_points[1], shape_points[2], shape_points[3])) + elseif #shape_points > 3 then -- find an ear -> a triangle with no other points inside it + for i, p1 in ipairs(shape_points) do + local p2 = shape_points[(i % #shape_points) + 1] + local p3 = shape_points[(i + 1) % #shape_points + 1] + local triangle = TRIANGLE:New(p1, p2, p3) + local is_ear = true + + if not is_clockwise(p1, p2, p3) then + is_ear = false + else + for _, point in ipairs(shape_points) do + if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then + is_ear = false + break + end + end + end + + if is_ear then + -- Check if any point in the original polygon is inside the ear triangle + local is_valid_triangle = true + for _, point in ipairs(points) do + if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then + is_valid_triangle = false + break + end + end + if is_valid_triangle then + table.insert(triangles, triangle) + local remaining_points = {} + for j, point in ipairs(shape_points) do + if point ~= p2 then + table.insert(remaining_points, point) + end + end + divide_recursively(remaining_points) + break + end + end + end + end + end + + points = ensure_clockwise(points) + divide_recursively(points) + return triangles +end + +function POLYGON:CovarianceMatrix() + local cx, cy = self:GetCentroid() + local covXX, covYY, covXY = 0, 0, 0 + for _, p in ipairs(self.points) do + covXX = covXX + (p.x - cx)^2 + covYY = covYY + (p.y - cy)^2 + covXY = covXY + (p.x - cx) * (p.y - cy) + end + covXX = covXX / (#self.points - 1) + covYY = covYY / (#self.points - 1) + covXY = covXY / (#self.points - 1) + return covXX, covYY, covXY +end + +function POLYGON:Direction() + local covXX, covYY, covXY = self:CovarianceMatrix() + -- Simplified calculation for the largest eigenvector's direction + local theta = 0.5 * math.atan2(2 * covXY, covXX - covYY) + return math.cos(theta), math.sin(theta) +end + +--- Returns a random Vec2 within the polygon. The Vec2 is weighted by the areas of the triangles that make up the polygon. +-- @return #table The random Vec2 +function POLYGON:GetRandomVec2() + local weights = {} + for _, triangle in pairs(self.Triangles) do + weights[triangle] = triangle.SurfaceArea / self.SurfaceArea + end + + local random_weight = math.random() + local accumulated_weight = 0 + for triangle, weight in pairs(weights) do + accumulated_weight = accumulated_weight + weight + if accumulated_weight >= random_weight then + return triangle:GetRandomVec2() + end + end +end + +--- Returns a random non-weighted Vec2 within the polygon. The Vec2 is chosen from one of the triangles that make up the polygon. +-- @return #table The random non-weighted Vec2 +function POLYGON:GetRandomNonWeightedVec2() + return self.Triangles[math.random(1, #self.Triangles)]:GetRandomVec2() +end + +--- Checks if a point is contained within the polygon. The point is a table with 'x' and 'y' fields. +-- @param #table point The point to check +-- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it +-- @return #bool True if the point is contained, false otherwise +function POLYGON:ContainsPoint(point, polygon_points) + local x = point.x + local y = point.y + + polygon_points = polygon_points or self.Points + + local counter = 0 + local num_points = #polygon_points + for current_index = 1, num_points do + local next_index = (current_index % num_points) + 1 + local current_x, current_y = polygon_points[current_index].x, polygon_points[current_index].y + local next_x, next_y = polygon_points[next_index].x, polygon_points[next_index].y + if ((current_y > y) ~= (next_y > y)) and (x < (next_x - current_x) * (y - current_y) / (next_y - current_y) + current_x) then + counter = counter + 1 + end + end + return counter % 2 == 1 +end + +--- Draws the polygon on the map. The polygon can be drawn with or without inner triangles. This is just for debugging +-- @param #bool include_inner_triangles Whether to include inner triangles in the drawing +function POLYGON:Draw(include_inner_triangles) + include_inner_triangles = include_inner_triangles or false + for i=1, #self.Coords do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.OutlineMarkIDs, c1:LineToAll(c2)) + end + + + if include_inner_triangles then + for _, triangle in ipairs(self.Triangles) do + triangle:Draw() + end + end +end + +--- Removes the drawing of the polygon from the map. +function POLYGON:RemoveDraw() + for _, triangle in pairs(self.Triangles) do + triangle:RemoveDraw() + end + for _, mark_id in pairs(self.OutlineMarkIDs) do + UTILS.RemoveMark(mark_id) + end +end + +--- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. +-- @return #number The surface area of the polygon +function POLYGON:__CalculateSurfaceArea() + local area = 0 + for _, triangle in pairs(self.Triangles) do + area = area + triangle.SurfaceArea + end + return area +end + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Moose Development/Moose/Shapes/ShapeBase.lua b/Moose Development/Moose/Shapes/ShapeBase.lua new file mode 100644 index 000000000..7042a44ad --- /dev/null +++ b/Moose Development/Moose/Shapes/ShapeBase.lua @@ -0,0 +1,216 @@ +--- **Shapes** - Class that serves as the base shapes drawn in the Mission Editor +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.SHAPE_BASE +-- @image CORE_Pathline.png + + +--- SHAPE_BASE class. +-- @type SHAPE_BASE +-- @field #string ClassName Name of the class. +-- @field #string Name Name of the shape +-- @field #table CenterVec2 Vec2 of the center of the shape, this will be assigned automatically +-- @field #table Points List of 3D points defining the shape, this will be assigned automatically +-- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically +-- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically +-- @extends Core.Base#BASE + +--- *I'm in love with the shape of you -- Ed Sheeran +-- +-- === +-- +-- # SHAPE_BASE +-- The class serves as the base class to deal with these shapes using MOOSE. You should never use this class on its own, +-- rather use: +-- CIRCLE +-- LINE +-- OVAL +-- POLYGON +-- TRIANGLE (although this one's a bit special as well) + +-- === +-- The idea is that anything you draw on the map in the Mission Editor can be turned in a shape to work with in MOOSE. +-- This is the base class that all other shape classes are built on. There are some shared functions, most of which are overridden in the derived classes + +-- @field #SHAPE_BASE + + +SHAPE_BASE = { + ClassName = "SHAPE_BASE", + Name = "", + CenterVec2 = nil, + Points = {}, + Coords = {}, + MarkIDs = {}, + ColorString = "", + ColorRGBA = {} +} + +--- Creates a new instance of SHAPE_BASE. +-- @return #SHAPE_BASE The new instance +function SHAPE_BASE:New() + local self = BASE:Inherit(self, BASE:New()) + return self +end + +--- Finds a shape on the map by its name. +-- @param #string shape_name Name of the shape to find +-- @return #SHAPE_BASE The found shape +function SHAPE_BASE:FindOnMap(shape_name) + local self = BASE:Inherit(self, BASE:New()) + + local found = false + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == shape_name then + self.Name = object["name"] + self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } + self.ColorString = object["colorString"] + self.ColorRGBA = UTILS.HexToRGBA(self.ColorString) + found = true + end + end + end + if not found then + self:E("Can't find a shape with name " .. shape_name) + end + return self +end + +function SHAPE_BASE:GetAllShapes(filter) + filter = filter or "" + local return_shapes = {} + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.contains(object["name"], filter) then + table.add(return_shapes, object) + end + end + end + + return return_shapes +end + +--- Offsets the shape to a new position. +-- @param #table new_vec2 The new position +function SHAPE_BASE:Offset(new_vec2) + local offset_vec2 = UTILS.Vec2Subtract(new_vec2, self.CenterVec2) + self.CenterVec2 = new_vec2 + if self.ClassName == "POLYGON" then + for _, point in pairs(self.Points) do + point.x = point.x + offset_vec2.x + point.y = point.y + offset_vec2.y + end + end +end + +--- Gets the name of the shape. +-- @return #string The name of the shape +function SHAPE_BASE:GetName() + return self.Name +end + +function SHAPE_BASE:GetColorString() + return self.ColorString +end + +function SHAPE_BASE:GetColorRGBA() + return self.ColorRGBA +end + +function SHAPE_BASE:GetColorRed() + return self.ColorRGBA.R +end + +function SHAPE_BASE:GetColorGreen() + return self.ColorRGBA.G +end + +function SHAPE_BASE:GetColorBlue() + return self.ColorRGBA.B +end + +function SHAPE_BASE:GetColorAlpha() + return self.ColorRGBA.A +end + +--- Gets the center position of the shape. +-- @return #table The center position +function SHAPE_BASE:GetCenterVec2() + return self.CenterVec2 +end + +--- Gets the center coordinate of the shape. +-- @return #COORDINATE The center coordinate +function SHAPE_BASE:GetCenterCoordinate() + return COORDINATE:NewFromVec2(self.CenterVec2) +end + +--- Gets the coordinate of the shape. +-- @return #COORDINATE The coordinate +function SHAPE_BASE:GetCoordinate() + return self:GetCenterCoordinate() +end + +--- Checks if a point is contained within the shape. +-- @param #table _ The point to check +-- @return #bool True if the point is contained, false otherwise +function SHAPE_BASE:ContainsPoint(_) + self:E("This needs to be set in the derived class") +end + +--- Checks if a unit is contained within the shape. +-- @param #string unit_name The name of the unit to check +-- @return #bool True if the unit is contained, false otherwise +function SHAPE_BASE:ContainsUnit(unit_name) + local unit = UNIT:FindByName(unit_name) + + if unit == nil or not unit:IsAlive() then + return false + end + + if self:ContainsPoint(unit:GetVec2()) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within the shape. +-- @param #string group_name The name of the group to check +-- @return #bool True if any unit of the group is contained, false otherwise +function SHAPE_BASE:ContainsAnyOfGroup(group_name) + local group = GROUP:FindByName(group_name) + + if group == nil or not group:IsAlive() then + return false + end + + for _, unit in pairs(group:GetUnits()) do + if self:ContainsPoint(unit:GetVec2()) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within the shape. +-- @param #string group_name The name of the group to check +-- @return #bool True if all units of the group are contained, false otherwise +function SHAPE_BASE:ContainsAllOfGroup(group_name) + local group = GROUP:FindByName(group_name) + + if group == nil or not group:IsAlive() then + return false + end + + for _, unit in pairs(group:GetUnits()) do + if not self:ContainsPoint(unit:GetVec2()) then + return false + end + end + return true +end diff --git a/Moose Development/Moose/Shapes/Triangle.lua b/Moose Development/Moose/Shapes/Triangle.lua new file mode 100644 index 000000000..c60b2aeef --- /dev/null +++ b/Moose Development/Moose/Shapes/Triangle.lua @@ -0,0 +1,86 @@ +-- TRIANGLE class with properties and methods for handling triangles. This class is mostly used by the POLYGON class, but you can use it on its own as well +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- +TRIANGLE = { + ClassName = "TRIANGLE", + Points = {}, + Coords = {}, + SurfaceArea = 0 +} + +--- Creates a new triangle from three points. The points need to be given as Vec2s +-- @param #table p1 The first point of the triangle +-- @param #table p2 The second point of the triangle +-- @param #table p3 The third point of the triangle +-- @return #TRIANGLE The new triangle +function TRIANGLE:New(p1, p2, p3) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.Points = {p1, p2, p3} + + local center_x = (p1.x + p2.x + p3.x) / 3 + local center_y = (p1.y + p2.y + p3.y) / 3 + self.CenterVec2 = {x=center_x, y=center_y} + + for _, pt in pairs({p1, p2, p3}) do + table.add(self.Coords, COORDINATE:NewFromVec2(pt)) + end + + self.SurfaceArea = math.abs((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) * 0.5 + + self.MarkIDs = {} + return self +end + +--- Checks if a point is contained within the triangle. +-- @param #table pt The point to check +-- @param #table points (optional) The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it +-- @return #bool True if the point is contained, false otherwise +function TRIANGLE:ContainsPoint(pt, points) + points = points or self.Points + + local function sign(p1, p2, p3) + return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y) + end + + local d1 = sign(pt, self.Points[1], self.Points[2]) + local d2 = sign(pt, self.Points[2], self.Points[3]) + local d3 = sign(pt, self.Points[3], self.Points[1]) + + local has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0) + local has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0) + + return not (has_neg and has_pos) +end + +--- Returns a random Vec2 within the triangle. +-- @param #table points The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it +-- @return #table The random Vec2 +function TRIANGLE:GetRandomVec2(points) + points = points or self.Points + local pt = {math.random(), math.random()} + table.sort(pt) + local s = pt[1] + local t = pt[2] - pt[1] + local u = 1 - pt[2] + + return {x = s * points[1].x + t * points[2].x + u * points[3].x, + y = s * points[1].y + t * points[2].y + u * points[3].y} +end + +--- Draws the triangle on the map, just for debugging +function TRIANGLE:Draw() + for i=1, #self.Coords do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.MarkIDs, c1:LineToAll(c2)) + end +end + +--- Removes the drawing of the triangle from the map. +function TRIANGLE:RemoveDraw() + for _, mark_id in pairs(self.MarkIDs) do + UTILS.RemoveMark(mark_id) + end +end diff --git a/Moose Development/Moose/Utilities/Utils.lua b/Moose Development/Moose/Utilities/Utils.lua index 6bf7e1fc9..96cf8c1b4 100644 --- a/Moose Development/Moose/Utilities/Utils.lua +++ b/Moose Development/Moose/Utilities/Utils.lua @@ -3513,6 +3513,25 @@ function string.contains(str, value) return string.match(str, value) end + +--- Moves an object from one table to another +-- @param #obj object to move +-- @param #from_table table to move from +-- @param #to_table table to move to +function table.move_object(obj, from_table, to_table) + local index + for i, v in pairs(from_table) do + if v == obj then + index = i + end + end + + if index then + local moved = table.remove(from_table, index) + table.insert_unique(to_table, moved) + end +end + --- Given tbl is a indexed table ({"hello", "dcs", "world"}), checks if element exists in the table. --- The table can be made up out of complex tables or values as well -- @param #table tbl @@ -3731,6 +3750,25 @@ function UTILS.OctalToDecimal(Number) return tonumber(Number,8) end + +--- HexToRGBA +-- @param hex_string table +-- @return #table R, G, B, A +function UTILS.HexToRGBA(hex_string) + local hexNumber = tonumber(string.sub(hex_string, 3), 16) -- convert the string to a number + -- extract RGBA components + local alpha = hexNumber % 256 + hexNumber = (hexNumber - alpha) / 256 + local blue = hexNumber % 256 + hexNumber = (hexNumber - blue) / 256 + local green = hexNumber % 256 + hexNumber = (hexNumber - green) / 256 + local red = hexNumber % 256 + + return {R = red, G = green, B = blue, A = alpha} +end + + --- Function to save the position of a set of #OPSGROUP (ARMYGROUP) objects. -- @param Core.Set#SET_OPSGROUP Set of ops objects to save -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. @@ -3768,7 +3806,7 @@ function UTILS.SaveSetOfOpsGroups(Set,Path,Filename,Structured) data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d,%s\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z,strucdata) else data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z) - end + end end end -- save the data @@ -3780,12 +3818,12 @@ end -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. -- @param #string Filename The name of the file. -- @return #table Returns a table of data entries: `{ groupname=groupname, size=size, coordinate=coordinate, template=template, structure=structure, legion=legion, alttemplate=alttemplate }` --- Returns nil when the file cannot be read. +-- Returns nil when the file cannot be read. function UTILS.LoadSetOfOpsGroups(Path,Filename) local filename = Filename or "SetOfGroups" local datatable = {} - + if UTILS.CheckFileExists(Path,filename) then local outcome,loadeddata = UTILS.LoadFromFile(Path,Filename) -- remove header @@ -3820,20 +3858,20 @@ end -- @param #number tgtHdg The absolute heading from the reference object to the target object/point in 0-360 -- @return #string text Text in clock heading such as "4 O'CLOCK" -- @usage Display the range and clock distance of a BTR in relation to REAPER 1-1's heading: --- +-- -- myUnit = UNIT:FindByName( "REAPER 1-1" ) -- myTarget = GROUP:FindByName( "BTR-1" ) --- +-- -- coordUnit = myUnit:GetCoordinate() -- coordTarget = myTarget:GetCoordinate() --- +-- -- hdgUnit = myUnit:GetHeading() -- hdgTarget = coordUnit:HeadingTo( coordTarget ) -- distTarget = coordUnit:Get3DDistance( coordTarget ) --- +-- -- clockString = UTILS.ClockHeadingString( hdgUnit, hdgTarget ) --- --- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! +-- +-- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! -- MESSAGE:New("Contact BTR at " .. clockString .. " for " .. distTarget .. "m!):ToUnit( myUnit ) function UTILS.ClockHeadingString(refHdg,tgtHdg) local relativeAngle = tgtHdg - refHdg From 892cb90d62f41cafeaad467489990fcb95f2f28c Mon Sep 17 00:00:00 2001 From: Thomas <72444570+Applevangelist@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:16:44 +0200 Subject: [PATCH 13/15] Adding Shapes (#2114) * Adding SHAPES (#2110) * Adding a new TerminalType (100)that seems to be introduced in the update that brought Muwaffaq Salti. The base has a couple of spots (like 04, 05, 06) that can only accommodate smaller type fixed wing aircraft, like the F-16, but not bigger types like the Warthog of the Strike Eagle. Because we weren't checking for this new type, spawning in these particular spots always resulted in an airstart, because `_CheckTerminalType` would always return `false` * Adding Shapes over from old MOOSE branch * cleanup * adding HEXtoRGBA * Revert "Adding SHAPES (#2110)" (#2112) This reverts commit 26deaca16632a2e16a854339f32170f0594f717d. * Adding SHAPES (#2113) * Adding a new TerminalType (100)that seems to be introduced in the update that brought Muwaffaq Salti. The base has a couple of spots (like 04, 05, 06) that can only accommodate smaller type fixed wing aircraft, like the F-16, but not bigger types like the Warthog of the Strike Eagle. Because we weren't checking for this new type, spawning in these particular spots always resulted in an airstart, because `_CheckTerminalType` would always return `false` * Adding Shapes over from old MOOSE branch * cleanup * adding HEXtoRGBA * removing Arrow.lua, it's part of Polygon.lua --------- Co-authored-by: Niels Vaes --- Moose Development/Moose/Modules.lua | 8 + Moose Development/Moose/Shapes/Circle.lua | 259 +++++++++++ Moose Development/Moose/Shapes/Cube.lua | 66 +++ Moose Development/Moose/Shapes/Line.lua | 331 ++++++++++++++ Moose Development/Moose/Shapes/Oval.lua | 213 +++++++++ Moose Development/Moose/Shapes/Polygon.lua | 458 +++++++++++++++++++ Moose Development/Moose/Shapes/ShapeBase.lua | 216 +++++++++ Moose Development/Moose/Shapes/Triangle.lua | 86 ++++ Moose Development/Moose/Utilities/Utils.lua | 56 ++- 9 files changed, 1684 insertions(+), 9 deletions(-) create mode 100644 Moose Development/Moose/Shapes/Circle.lua create mode 100644 Moose Development/Moose/Shapes/Cube.lua create mode 100644 Moose Development/Moose/Shapes/Line.lua create mode 100644 Moose Development/Moose/Shapes/Oval.lua create mode 100644 Moose Development/Moose/Shapes/Polygon.lua create mode 100644 Moose Development/Moose/Shapes/ShapeBase.lua create mode 100644 Moose Development/Moose/Shapes/Triangle.lua diff --git a/Moose Development/Moose/Modules.lua b/Moose Development/Moose/Modules.lua index a380e6528..1710b59d1 100644 --- a/Moose Development/Moose/Modules.lua +++ b/Moose Development/Moose/Modules.lua @@ -154,6 +154,14 @@ __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Route.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Account.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Actions/Act_Assist.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/ShapeBase.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Circle.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Cube.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Line.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Oval.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Polygon.lua' ) +__Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Shapes/Triangle.lua' ) + __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/UserSound.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/SoundOutput.lua' ) __Moose.Include( MOOSE_DEVELOPMENT_FOLDER..'/Moose/Sound/Radio.lua' ) diff --git a/Moose Development/Moose/Shapes/Circle.lua b/Moose Development/Moose/Shapes/Circle.lua new file mode 100644 index 000000000..04c153d86 --- /dev/null +++ b/Moose Development/Moose/Shapes/Circle.lua @@ -0,0 +1,259 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.CIRCLE + +--- CIRCLE class. +-- @type CIRCLE +-- @field #string ClassName Name of the class. +-- @field #number Radius Radius of the circle + +--- *It's NOT hip to be square* -- Someone, somewhere, probably +-- +-- === +-- +-- # CIRCLE +-- CIRCLEs can be fetched from the drawings in the Mission Editor + +-- This class has some of the standard CIRCLE functions you'd expect. One function of interest is CIRCLE:PointInSector() that you can use if a point is +-- within a certain sector (pizza slice) of a circle. This can be useful for many things, including rudimentary, "radar-like" searches from a unit. + +-- @field #CIRCLE + +--- CIRCLE class with properties and methods for handling circles. +CIRCLE = { + ClassName = "CIRCLE", + Radius = nil, +} +--- Finds a circle on the map by its name. The circle must have been added in the Mission Editor +-- @param #string shape_name Name of the circle to find +-- @return #CIRCLE The found circle, or nil if not found +function CIRCLE:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.find(object["name"], shape_name, 1, true) then + if object["polygonMode"] == "circle" then + self.Radius = object["radius"] + end + end + end + end + + return self +end + +--- Finds a circle by its name in the database. +-- @param #string shape_name Name of the circle to find +-- @return #CIRCLE The found circle, or nil if not found +function CIRCLE:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new circle from a center point and a radius. +-- @param #table vec2 The center point of the circle +-- @param #number radius The radius of the circle +-- @return #CIRCLE The new circle +function CIRCLE:New(vec2, radius) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = vec2 + self.Radius = radius + return self +end + +--- Gets the radius of the circle. +-- @return #number The radius of the circle +function CIRCLE:GetRadius() + return self.Radius +end + +--- Checks if a point is contained within the circle. +-- @param #table point The point to check +-- @return #bool True if the point is contained, false otherwise +function CIRCLE:ContainsPoint(point) + if ((point.x - self.CenterVec2.x) ^ 2 + (point.y - self.CenterVec2.y) ^ 2) ^ 0.5 <= self.Radius then + return true + end + return false +end + +--- Checks if a point is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #table point The point to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if the point is contained, false otherwise +function CIRCLE:PointInSector(point, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + local function are_clockwise(v1, v2) + return -v1.x * v2.y + v1.y * v2.x > 0 + end + + local function is_in_radius(rp) + return rp.x * rp.x + rp.y * rp.y <= radius ^ 2 + end + + local rel_pt = { + x = point.x - center.x, + y = point.y - center.y + } + + local rel_sector_start = { + x = sector_start.x - center.x, + y = sector_start.y - center.y, + } + + local rel_sector_end = { + x = sector_end.x - center.x, + y = sector_end.y - center.y, + } + + return not are_clockwise(rel_sector_start, rel_pt) and + are_clockwise(rel_sector_end, rel_pt) and + is_in_radius(rel_pt, radius) +end + +--- Checks if a unit is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string unit_name The name of the unit to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if the unit is contained, false otherwise +function CIRCLE:UnitInSector(unit_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + if self:PointInSector(UNIT:FindByName(unit_name):GetVec2(), sector_start, sector_end, center, radius) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string group_name The name of the group to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if any unit of the group is contained, false otherwise +function CIRCLE:AnyOfGroupInSector(group_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within a sector of the circle. The start and end sector need to be clockwise +-- @param #string group_name The name of the group to check +-- @param #table sector_start The start point of the sector +-- @param #table sector_end The end point of the sector +-- @param #table center The center point of the sector +-- @param #number radius The radius of the sector +-- @return #bool True if all units of the group are contained, false otherwise +function CIRCLE:AllOfGroupInSector(group_name, sector_start, sector_end, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if not self:PointInSector(unit:GetVec2(), sector_start, sector_end, center, radius) then + return false + end + end + return true +end + +--- Checks if a unit is contained within a radius of the circle. +-- @param #string unit_name The name of the unit to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if the unit is contained, false otherwise +function CIRCLE:UnitInRadius(unit_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + if UTILS.IsInRadius(center, UNIT:FindByName(unit_name):GetVec2(), radius) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within a radius of the circle. +-- @param #string group_name The name of the group to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if any unit of the group is contained, false otherwise +function CIRCLE:AnyOfGroupInRadius(group_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if UTILS.IsInRadius(center, unit:GetVec2(), radius) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within a radius of the circle. +-- @param #string group_name The name of the group to check +-- @param #table center The center point of the radius +-- @param #number radius The radius to check +-- @return #bool True if all units of the group are contained, false otherwise +function CIRCLE:AllOfGroupInRadius(group_name, center, radius) + center = center or self.CenterVec2 + radius = radius or self.Radius + + for _, unit in pairs(GROUP:FindByName(group_name):GetUnits()) do + if not UTILS.IsInRadius(center, unit:GetVec2(), radius) then + return false + end + end + return true +end + +--- Returns a random Vec2 within the circle. +-- @return #table The random Vec2 +function CIRCLE:GetRandomVec2() + local angle = math.random() * 2 * math.pi + + local rx = math.random(0, self.Radius) * math.cos(angle) + self.CenterVec2.x + local ry = math.random(0, self.Radius) * math.sin(angle) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Returns a random Vec2 on the border of the circle. +-- @return #table The random Vec2 +function CIRCLE:GetRandomVec2OnBorder() + local angle = math.random() * 2 * math.pi + + local rx = self.Radius * math.cos(angle) + self.CenterVec2.x + local ry = self.Radius * math.sin(angle) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Calculates the bounding box of the circle. The bounding box is the smallest rectangle that contains the circle. +-- @return #table The bounding box of the circle +function CIRCLE:GetBoundingBox() + local min_x = self.CenterVec2.x - self.Radius + local min_y = self.CenterVec2.y - self.Radius + local max_x = self.CenterVec2.x + self.Radius + local max_y = self.CenterVec2.y + self.Radius + + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + diff --git a/Moose Development/Moose/Shapes/Cube.lua b/Moose Development/Moose/Shapes/Cube.lua new file mode 100644 index 000000000..ae3f73090 --- /dev/null +++ b/Moose Development/Moose/Shapes/Cube.lua @@ -0,0 +1,66 @@ +CUBE = { + ClassName = "CUBE", + Points = {}, + Coords = {} +} + +--- Points need to be added in the following order: +--- p1 -> p4 make up the front face of the cube +--- p5 -> p8 make up the back face of the cube +--- p1 connects to p5 +--- p2 connects to p6 +--- p3 connects to p7 +--- p4 connects to p8 +--- +--- 8-----------7 +--- /| /| +--- / | / | +--- 4--+--------3 | +--- | | | | +--- | | | | +--- | | | | +--- | 5--------+--6 +--- | / | / +--- |/ |/ +--- 1-----------2 +--- +function CUBE:New(p1, p2, p3, p4, p5, p6, p7, p8) + local self = BASE:Inherit(self, SHAPE_BASE) + self.Points = {p1, p2, p3, p4, p5, p6, p7, p8} + for _, point in spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec3(point)) + end + return self +end + +function CUBE:GetCenter() + local center = { x=0, y=0, z=0 } + for _, point in pairs(self.Points) do + center.x = center.x + point.x + center.y = center.y + point.y + center.z = center.z + point.z + end + + center.x = center.x / 8 + center.y = center.y / 8 + center.z = center.z / 8 + return center +end + +function CUBE:ContainsPoint(point, cube_points) + cube_points = cube_points or self.Points + local min_x, min_y, min_z = math.huge, math.huge, math.huge + local max_x, max_y, max_z = -math.huge, -math.huge, -math.huge + + -- Find the minimum and maximum x, y, and z values of the cube points + for _, p in ipairs(cube_points) do + if p.x < min_x then min_x = p.x end + if p.y < min_y then min_y = p.y end + if p.z < min_z then min_z = p.z end + if p.x > max_x then max_x = p.x end + if p.y > max_y then max_y = p.y end + if p.z > max_z then max_z = p.z end + end + + return point.x >= min_x and point.x <= max_x and point.y >= min_y and point.y <= max_y and point.z >= min_z and point.z <= max_z +end diff --git a/Moose Development/Moose/Shapes/Line.lua b/Moose Development/Moose/Shapes/Line.lua new file mode 100644 index 000000000..08f7c84a0 --- /dev/null +++ b/Moose Development/Moose/Shapes/Line.lua @@ -0,0 +1,331 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.LINE + +--- OVAL class. +-- @type OVAL +-- @field #string ClassName Name of the class. +-- @field #number Points points of the line +-- @field #number Coords coordinates of the line + +-- +-- === + +-- @field #LINE +LINE = { + ClassName = "LINE", + Points = {}, + Coords = {}, +} + +--- Finds a line on the map by its name. The line must be drawn in the Mission Editor +-- @param #string line_name Name of the line to find +-- @return #LINE The found line, or nil if not found +function LINE:FindOnMap(line_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(line_name)) + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == line_name then + if object["primitiveType"] == "Line" then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + table.insert(self.Points, p) + table.insert(self.Coords, coord) + end + end + end + end + end + + self:I(#self.Points) + if #self.Points == 0 then + return nil + end + + self.MarkIDs = {} + + return self +end + +--- Finds a line by its name in the database. +-- @param #string shape_name Name of the line to find +-- @return #LINE The found line, or nil if not found +function LINE:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new line from two points. +-- @param #table vec2 The first point of the line +-- @param #number radius The second point of the line +-- @return #LINE The new line +function LINE:New(...) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.Points = {...} + self:I(self.Points) + for _, point in UTILS.spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + return self +end + +--- Creates a new line from a circle. +-- @param #table center_point center point of the circle +-- @param #number radius radius of the circle, half length of the line +-- @param #number angle_degrees degrees the line will form from center point +-- @return #LINE The new line +function LINE:NewFromCircle(center_point, radius, angle_degrees) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = center_point + local angleRadians = math.rad(angle_degrees) + + local point1 = { + x = center_point.x + radius * math.cos(angleRadians), + y = center_point.y + radius * math.sin(angleRadians) + } + + local point2 = { + x = center_point.x + radius * math.cos(angleRadians + math.pi), + y = center_point.y + radius * math.sin(angleRadians + math.pi) + } + + for _, point in pairs{point1, point2} do + table.insert(self.Points, point) + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + + return self +end + +--- Gets the coordinates of the line. +-- @return #table The coordinates of the line +function LINE:Coordinates() + return self.Coords +end + +--- Gets the start coordinate of the line. The start coordinate is the first point of the line. +-- @return #COORDINATE The start coordinate of the line +function LINE:GetStartCoordinate() + return self.Coords[1] +end + +--- Gets the end coordinate of the line. The end coordinate is the last point of the line. +-- @return #COORDINATE The end coordinate of the line +function LINE:GetEndCoordinate() + return self.Coords[#self.Coords] +end + +--- Gets the start point of the line. The start point is the first point of the line. +-- @return #table The start point of the line +function LINE:GetStartPoint() + return self.Points[1] +end + +--- Gets the end point of the line. The end point is the last point of the line. +-- @return #table The end point of the line +function LINE:GetEndPoint() + return self.Points[#self.Points] +end + +--- Gets the length of the line. +-- @return #number The length of the line +function LINE:GetLength() + local total_length = 0 + for i=1, #self.Points - 1 do + local x1, y1 = self.Points[i]["x"], self.Points[i]["y"] + local x2, y2 = self.Points[i+1]["x"], self.Points[i+1]["y"] + local segment_length = math.sqrt((x2 - x1)^2 + (y2 - y1)^2) + total_length = total_length + segment_length + end + return total_length +end + +--- Returns a random point on the line. +-- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it +-- @return #table The random point +function LINE:GetRandomPoint(points) + points = points or self.Points + local rand = math.random() -- 0->1 + + local random_x = points[1].x + rand * (points[2].x - points[1].x) + local random_y = points[1].y + rand * (points[2].y - points[1].y) + + return { x= random_x, y= random_y } +end + +--- Gets the heading of the line. +-- @param #table points (optional) The points of the line or 2 other points if you're just using the LINE class without an object of it +-- @return #number The heading of the line +function LINE:GetHeading(points) + points = points or self.Points + + local angle = math.atan2(points[2].y - points[1].y, points[2].x - points[1].x) + + angle = math.deg(angle) + if angle < 0 then + angle = angle + 360 + end + + return angle +end + + +--- Return each part of the line as a new line +-- @return #table The points +function LINE:GetIndividualParts() + local parts = {} + if #self.Points == 2 then + parts = {self} + end + + for i=1, #self.Points -1 do + local p1 = self.Points[i] + local p2 = self.Points[i % #self.Points + 1] + table.add(parts, LINE:New(p1, p2)) + end + + return parts +end + +--- Gets a number of points in between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @return #table The points +function LINE:GetPointsInbetween(amount, start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + if amount == 0 then return {start_point, end_point} end + + amount = amount + 1 + local points = {} + + local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } + local divided = { x = difference.x / amount, y = difference.y / amount } + + for j=0, amount do + local part_pos = {x = divided.x * j, y = divided.y * j} + -- add part_pos vector to the start point so the new point is placed along in the line + local point = {x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} + table.insert(points, point) + end + return points +end + +--- Gets a number of points in between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @return #table The points +function LINE:GetCoordinatesInBetween(amount, start_point, end_point) + local coords = {} + for _, pt in pairs(self:GetPointsInbetween(amount, start_point, end_point)) do + table.add(coords, COORDINATE:NewFromVec2(pt)) + end + return coords +end + + +function LINE:GetRandomPoint(start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + + local fraction = math.random() + + local difference = { x = end_point.x - start_point.x, y = end_point.y - start_point.y } + local part_pos = {x = difference.x * fraction, y = difference.y * fraction} + local random_point = { x = start_point.x + part_pos.x, y = start_point.y + part_pos.y} + + return random_point +end + + +function LINE:GetRandomCoordinate(start_point, end_point) + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + + return COORDINATE:NewFromVec2(self:GetRandomPoint(start_point, end_point)) +end + + +--- Gets a number of points on a sine wave between the start and end points of the line. +-- @param #number amount The number of points to get +-- @param #table start_point (Optional) The start point of the line, defaults to the object's start point +-- @param #table end_point (Optional) The end point of the line, defaults to the object's end point +-- @param #number frequency (Optional) The frequency of the sine wave, default 1 +-- @param #number phase (Optional) The phase of the sine wave, default 0 +-- @param #number amplitude (Optional) The amplitude of the sine wave, default 100 +-- @return #table The points +function LINE:GetPointsBetweenAsSineWave(amount, start_point, end_point, frequency, phase, amplitude) + amount = amount or 20 + start_point = start_point or self:GetStartPoint() + end_point = end_point or self:GetEndPoint() + frequency = frequency or 1 -- number of cycles per unit of x + phase = phase or 0 -- offset in radians + amplitude = amplitude or 100 -- maximum height of the wave + + local points = {} + + -- Returns the y-coordinate of the sine wave at x + local function sine_wave(x) + return amplitude * math.sin(2 * math.pi * frequency * (x - start_point.x) + phase) + end + + -- Plot x-amount of points on the sine wave between point_01 and point_02 + local x = start_point.x + local step = (end_point.x - start_point.x) / 20 + for _=1, amount do + local y = sine_wave(x) + x = x + step + table.add(points, {x=x, y=y}) + end + return points +end + +--- Calculates the bounding box of the line. The bounding box is the smallest rectangle that contains the line. +-- @return #table The bounding box of the line +function LINE:GetBoundingBox() + local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[2].x, self.Points[2].y + + for i = 2, #self.Points do + local x, y = self.Points[i].x, self.Points[i].y + + if x < min_x then + min_x = x + end + if y < min_y then + min_y = y + end + if x > max_x then + max_x = x + end + if y > max_y then + max_y = y + end + end + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Draws the line on the map. +-- @param #table points The points of the line +function LINE:Draw() + for i=1, #self.Coords -1 do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.MarkIDs, c1:LineToAll(c2)) + end +end + +--- Removes the drawing of the line from the map. +function LINE:RemoveDraw() + for _, mark_id in pairs(self.MarkIDs) do + UTILS.RemoveMark(mark_id) + end +end diff --git a/Moose Development/Moose/Shapes/Oval.lua b/Moose Development/Moose/Shapes/Oval.lua new file mode 100644 index 000000000..d2f85a822 --- /dev/null +++ b/Moose Development/Moose/Shapes/Oval.lua @@ -0,0 +1,213 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.OVAL + +--- OVAL class. +-- @type OVAL +-- @field #string ClassName Name of the class. +-- @field #number MajorAxis The major axis (radius) of the oval +-- @field #number MinorAxis The minor axis (radius) of the oval +-- @field #number Angle The angle the oval is rotated on + +--- *The little man removed his hat, what an egg shaped head he had* -- Agatha Christie +-- +-- === +-- +-- # OVAL +-- OVALs can be fetched from the drawings in the Mission Editor + +-- The major and minor axes define how elongated the shape of an oval is. This class has some basic functions that the other SHAPE classes have as well. +-- Since it's not possible to draw the shape of an oval while the mission is running, right now the draw function draws 2 cicles. One with the major axis and one with +-- the minor axis. It then draws a diamond shape on an angle where the corners touch the major and minor axes to give an indication of what the oval actually +-- looks like. + +-- Using ovals can be handy to find an area on the ground that is actually an intersection of a cone and a plane. So imagine you're faking the view cone of +-- a targeting pod and + +-- @field #OVAL + +--- OVAL class with properties and methods for handling ovals. +OVAL = { + ClassName = "OVAL", + MajorAxis = nil, + MinorAxis = nil, + Angle = 0, + DrawPoly=nil +} + +--- Finds an oval on the map by its name. The oval must be drawn on the map. +-- @param #string shape_name Name of the oval to find +-- @return #OVAL The found oval, or nil if not found +function OVAL:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.find(object["name"], shape_name, 1, true) then + if object["polygonMode"] == "oval" then + self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } + self.MajorAxis = object["r1"] + self.MinorAxis = object["r2"] + self.Angle = object["angle"] + end + end + end + end + + return self +end + +--- Finds an oval by its name in the database. +-- @param #string shape_name Name of the oval to find +-- @return #OVAL The found oval, or nil if not found +function OVAL:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new oval from a center point, major axis, minor axis, and angle. +-- @param #table vec2 The center point of the oval +-- @param #number major_axis The major axis of the oval +-- @param #number minor_axis The minor axis of the oval +-- @param #number angle The angle of the oval +-- @return #OVAL The new oval +function OVAL:New(vec2, major_axis, minor_axis, angle) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.CenterVec2 = vec2 + self.MajorAxis = major_axis + self.MinorAxis = minor_axis + self.Angle = angle or 0 + + return self +end + +--- Gets the major axis of the oval. +-- @return #number The major axis of the oval +function OVAL:GetMajorAxis() + return self.MajorAxis +end + +--- Gets the minor axis of the oval. +-- @return #number The minor axis of the oval +function OVAL:GetMinorAxis() + return self.MinorAxis +end + +--- Gets the angle of the oval. +-- @return #number The angle of the oval +function OVAL:GetAngle() + return self.Angle +end + +--- Sets the major axis of the oval. +-- @param #number value The new major axis +function OVAL:SetMajorAxis(value) + self.MajorAxis = value +end + +--- Sets the minor axis of the oval. +-- @param #number value The new minor axis +function OVAL:SetMinorAxis(value) + self.MinorAxis = value +end + +--- Sets the angle of the oval. +-- @param #number value The new angle +function OVAL:SetAngle(value) + self.Angle = value +end + +--- Checks if a point is contained within the oval. +-- @param #table point The point to check +-- @return #bool True if the point is contained, false otherwise +function OVAL:ContainsPoint(point) + local cos, sin = math.cos, math.sin + local dx = point.x - self.CenterVec2.x + local dy = point.y - self.CenterVec2.y + local rx = dx * cos(self.Angle) + dy * sin(self.Angle) + local ry = -dx * sin(self.Angle) + dy * cos(self.Angle) + return rx * rx / (self.MajorAxis * self.MajorAxis) + ry * ry / (self.MinorAxis * self.MinorAxis) <= 1 +end + +--- Returns a random Vec2 within the oval. +-- @return #table The random Vec2 +function OVAL:GetRandomVec2() + local theta = math.rad(self.Angle) + + local random_point = math.sqrt(math.random()) --> uniformly + --local random_point = math.random() --> more clumped around center + local phi = math.random() * 2 * math.pi + local x_c = random_point * math.cos(phi) + local y_c = random_point * math.sin(phi) + local x_e = x_c * self.MajorAxis + local y_e = y_c * self.MinorAxis + local rx = (x_e * math.cos(theta) - y_e * math.sin(theta)) + self.CenterVec2.x + local ry = (x_e * math.sin(theta) + y_e * math.cos(theta)) + self.CenterVec2.y + + return {x=rx, y=ry} +end + +--- Calculates the bounding box of the oval. The bounding box is the smallest rectangle that contains the oval. +-- @return #table The bounding box of the oval +function OVAL:GetBoundingBox() + local min_x = self.CenterVec2.x - self.MajorAxis + local min_y = self.CenterVec2.y - self.MinorAxis + local max_x = self.CenterVec2.x + self.MajorAxis + local max_y = self.CenterVec2.y + self.MinorAxis + + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Draws the oval on the map, for debugging +-- @param #number angle (Optional) The angle of the oval. If nil will use self.Angle +function OVAL:Draw() + --for pt in pairs(self:PointsOnEdge(20)) do + -- COORDINATE:NewFromVec2(pt) + --end + + self.DrawPoly = POLYGON:NewFromPoints(self:PointsOnEdge(20)) + self.DrawPoly:Draw(true) + + + + + ---- TODO: draw a better shape using line segments + --angle = angle or self.Angle + --local coor = self:GetCenterCoordinate() + -- + --table.add(self.MarkIDs, coor:CircleToAll(self.MajorAxis)) + --table.add(self.MarkIDs, coor:CircleToAll(self.MinorAxis)) + --table.add(self.MarkIDs, coor:LineToAll(coor:Translate(self.MajorAxis, self.Angle))) + -- + --local pt_1 = coor:Translate(self.MajorAxis, self.Angle) + --local pt_2 = coor:Translate(self.MinorAxis, self.Angle - 90) + --local pt_3 = coor:Translate(self.MajorAxis, self.Angle - 180) + --local pt_4 = coor:Translate(self.MinorAxis, self.Angle - 270) + --table.add(self.MarkIDs, pt_1:QuadToAll(pt_2, pt_3, pt_4), -1, {0, 1, 0}, 1, {0, 1, 0}) +end + +--- Removes the drawing of the oval from the map +function OVAL:RemoveDraw() + self.DrawPoly:RemoveDraw() +end + + +function OVAL:PointsOnEdge(num_points) + num_points = num_points or 20 + local points = {} + local dtheta = 2 * math.pi / num_points + + for i = 0, num_points - 1 do + local theta = i * dtheta + local x = self.CenterVec2.x + self.MajorAxis * math.cos(theta) * math.cos(self.Angle) - self.MinorAxis * math.sin(theta) * math.sin(self.Angle) + local y = self.CenterVec2.y + self.MajorAxis * math.cos(theta) * math.sin(self.Angle) + self.MinorAxis * math.sin(theta) * math.cos(self.Angle) + table.insert(points, {x = x, y = y}) + end + + return points +end + + diff --git a/Moose Development/Moose/Shapes/Polygon.lua b/Moose Development/Moose/Shapes/Polygon.lua new file mode 100644 index 000000000..a40256ecf --- /dev/null +++ b/Moose Development/Moose/Shapes/Polygon.lua @@ -0,0 +1,458 @@ +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.POLYGON + +--- POLYGON class. +-- @type POLYGON +-- @field #string ClassName Name of the class. +-- @field #table Points List of 3D points defining the shape, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically if you're passing in a drawing from the Mission Editor +-- @field #table Triangles List of TRIANGLEs that make up the shape of the POLYGON after being triangulated +-- @extends Core.Base#BASE + +--- *Polygons are fashionable at the moment* -- Trip Hawkins +-- +-- === +-- +-- # POLYGON +-- POLYGONs can be fetched from the drawings in the Mission Editor if the drawing is: +-- * A closed shape made with line segments +-- * A closed shape made with a freehand line +-- * A freehand drawn polygon +-- * A rect +-- Use the POLYGON:FindOnMap() of POLYGON:Find() functions for this. You can also create a non existing polygon in memory using the POLYGON:New() function. Pass in a +-- any number of Vec2s into this function to define the shape of the polygon you want. + +-- You can draw very intricate and complex polygons in the Mission Editor to avoid (or include) map objects. You can then generate random points within this complex +-- shape for spawning groups or checking positions. + +-- When a POLYGON is made, it's automatically triangulated. The resulting triangles are stored in POLYGON.Triangles. This also immeadiately saves the surface area +-- of the POLYGON. Because the POLYGON is triangulated, it's possible to generate random points within this POLYGON without having to use a trial and error method to see if +-- the point is contained within the shape. +-- Using POLYGON:GetRandomVec2() will result in a truly, non-biased, random Vec2 within the shape. You'll want to use this function most. There's also POLYGON:GetRandomNonWeightedVec2 +-- which ignores the size of the triangles in the polygon to pick a random points. This will result in more points clumping together in parts of the polygon where the triangles are +-- the smallest. + + +-- @field #POLYGON + +POLYGON = { + ClassName = "POLYGON", + Points = {}, + Coords = {}, + Triangles = {}, + SurfaceArea = 0, + TriangleMarkIDs = {}, + OutlineMarkIDs = {}, + Angle = nil, -- for arrows + Heading = nil -- for arrows +} + +--- Finds a polygon on the map by its name. The polygon must be added in the mission editor. +-- @param #string shape_name Name of the polygon to find +-- @return #POLYGON The found polygon, or nil if not found +function POLYGON:FindOnMap(shape_name) + local self = BASE:Inherit(self, SHAPE_BASE:FindOnMap(shape_name)) + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == shape_name then + if (object["primitiveType"] == "Line" and object["closed"] == true) or (object["polygonMode"] == "free") then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + self.Points[#self.Points + 1] = p + self.Coords[#self.Coords + 1] = coord + end + elseif object["polygonMode"] == "rect" then + local angle = object["angle"] + local half_width = object["width"] / 2 + local half_height = object["height"] / 2 + + local p1 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) + local p2 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y + half_width }, self.CenterVec2, angle) + local p3 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x + half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) + local p4 = UTILS.RotatePointAroundPivot({ x = self.CenterVec2.x - half_height, y = self.CenterVec2.y - half_width }, self.CenterVec2, angle) + + self.Points = {p1, p2, p3, p4} + for _, point in pairs(self.Points) do + self.Coords[#self.Coords + 1] = COORDINATE:NewFromVec2(point) + end + elseif object["polygonMode"] == "arrow" then + for _, point in UTILS.spairs(object["points"]) do + local p = {x = object["mapX"] + point["x"], + y = object["mapY"] + point["y"] } + local coord = COORDINATE:NewFromVec2(p) + self.Points[#self.Points + 1] = p + self.Coords[#self.Coords + 1] = coord + end + self.Angle = object["angle"] + self.Heading = UTILS.ClampAngle(self.Angle + 90) + end + end + end + end + + if #self.Points == 0 then + return nil + end + + self.CenterVec2 = self:GetCentroid() + self.Triangles = self:Triangulate() + self.SurfaceArea = self:__CalculateSurfaceArea() + + self.TriangleMarkIDs = {} + self.OutlineMarkIDs = {} + return self +end + +--- Creates a polygon from a zone. The zone must be defined in the mission. +-- @param #string zone_name Name of the zone +-- @return #POLYGON The polygon created from the zone, or nil if the zone is not found +function POLYGON:FromZone(zone_name) + for _, zone in pairs(env.mission.triggers.zones) do + if zone["name"] == zone_name then + return POLYGON:New(unpack(zone["verticies"] or {})) + end + end +end + +--- Finds a polygon by its name in the database. +-- @param #string shape_name Name of the polygon to find +-- @return #POLYGON The found polygon, or nil if not found +function POLYGON:Find(shape_name) + return _DATABASE:FindShape(shape_name) +end + +--- Creates a new polygon from a list of points. Each point is a table with 'x' and 'y' fields. +-- @param #table ... Points of the polygon +-- @return #POLYGON The new polygon +function POLYGON:New(...) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + + self.Points = {...} + self.Coords = {} + for _, point in UTILS.spairs(self.Points) do + table.insert(self.Coords, COORDINATE:NewFromVec2(point)) + end + self.Triangles = self:Triangulate() + self.SurfaceArea = self:__CalculateSurfaceArea() + + return self +end + +--- Calculates the centroid of the polygon. The centroid is the average of the 'x' and 'y' coordinates of the points. +-- @return #table The centroid of the polygon +function POLYGON:GetCentroid() + local function sum(t) + local total = 0 + for _, value in pairs(t) do + total = total + value + end + return total + end + + local x_values = {} + local y_values = {} + local length = table.length(self.Points) + + for _, point in pairs(self.Points) do + table.insert(x_values, point.x) + table.insert(y_values, point.y) + end + + local x = sum(x_values) / length + local y = sum(y_values) / length + + return { + ["x"] = x, + ["y"] = y + } +end + +--- Returns the coordinates of the polygon. Each coordinate is a COORDINATE object. +-- @return #table The coordinates of the polygon +function POLYGON:GetCoordinates() + return self.Coords +end + +--- Returns the start coordinate of the polygon. The start coordinate is the first point of the polygon. +-- @return #COORDINATE The start coordinate of the polygon +function POLYGON:GetStartCoordinate() + return self.Coords[1] +end + +--- Returns the end coordinate of the polygon. The end coordinate is the last point of the polygon. +-- @return #COORDINATE The end coordinate of the polygon +function POLYGON:GetEndCoordinate() + return self.Coords[#self.Coords] +end + +--- Returns the start point of the polygon. The start point is the first point of the polygon. +-- @return #table The start point of the polygon +function POLYGON:GetStartPoint() + return self.Points[1] +end + +--- Returns the end point of the polygon. The end point is the last point of the polygon. +-- @return #table The end point of the polygon +function POLYGON:GetEndPoint() + return self.Points[#self.Points] +end + +--- Returns the points of the polygon. Each point is a table with 'x' and 'y' fields. +-- @return #table The points of the polygon +function POLYGON:GetPoints() + return self.Points +end + +--- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. +-- @return #number The surface area of the polygon +function POLYGON:GetSurfaceArea() + return self.SurfaceArea +end + +--- Calculates the bounding box of the polygon. The bounding box is the smallest rectangle that contains the polygon. +-- @return #table The bounding box of the polygon +function POLYGON:GetBoundingBox() + local min_x, min_y, max_x, max_y = self.Points[1].x, self.Points[1].y, self.Points[1].x, self.Points[1].y + + for i = 2, #self.Points do + local x, y = self.Points[i].x, self.Points[i].y + + if x < min_x then + min_x = x + end + if y < min_y then + min_y = y + end + if x > max_x then + max_x = x + end + if y > max_y then + max_y = y + end + end + return { + {x=min_x, y=min_x}, {x=max_x, y=min_y}, {x=max_x, y=max_y}, {x=min_x, y=max_y} + } +end + +--- Triangulates the polygon. The polygon is divided into triangles. +-- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it +-- @return #table The triangles of the polygon +function POLYGON:Triangulate(points) + points = points or self.Points + local triangles = {} + + local function get_orientation(shape_points) + local sum = 0 + for i = 1, #shape_points do + local j = i % #shape_points + 1 + sum = sum + (shape_points[j].x - shape_points[i].x) * (shape_points[j].y + shape_points[i].y) + end + return sum >= 0 and "clockwise" or "counter-clockwise" -- sum >= 0, return "clockwise", else return "counter-clockwise" + end + + local function ensure_clockwise(shape_points) + local orientation = get_orientation(shape_points) + if orientation == "counter-clockwise" then + -- Reverse the order of shape_points so they're clockwise + local reversed = {} + for i = #shape_points, 1, -1 do + table.insert(reversed, shape_points[i]) + end + return reversed + end + return shape_points + end + + local function is_clockwise(p1, p2, p3) + local cross_product = (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x) + return cross_product < 0 + end + + local function divide_recursively(shape_points) + if #shape_points == 3 then + table.insert(triangles, TRIANGLE:New(shape_points[1], shape_points[2], shape_points[3])) + elseif #shape_points > 3 then -- find an ear -> a triangle with no other points inside it + for i, p1 in ipairs(shape_points) do + local p2 = shape_points[(i % #shape_points) + 1] + local p3 = shape_points[(i + 1) % #shape_points + 1] + local triangle = TRIANGLE:New(p1, p2, p3) + local is_ear = true + + if not is_clockwise(p1, p2, p3) then + is_ear = false + else + for _, point in ipairs(shape_points) do + if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then + is_ear = false + break + end + end + end + + if is_ear then + -- Check if any point in the original polygon is inside the ear triangle + local is_valid_triangle = true + for _, point in ipairs(points) do + if point ~= p1 and point ~= p2 and point ~= p3 and triangle:ContainsPoint(point) then + is_valid_triangle = false + break + end + end + if is_valid_triangle then + table.insert(triangles, triangle) + local remaining_points = {} + for j, point in ipairs(shape_points) do + if point ~= p2 then + table.insert(remaining_points, point) + end + end + divide_recursively(remaining_points) + break + end + end + end + end + end + + points = ensure_clockwise(points) + divide_recursively(points) + return triangles +end + +function POLYGON:CovarianceMatrix() + local cx, cy = self:GetCentroid() + local covXX, covYY, covXY = 0, 0, 0 + for _, p in ipairs(self.points) do + covXX = covXX + (p.x - cx)^2 + covYY = covYY + (p.y - cy)^2 + covXY = covXY + (p.x - cx) * (p.y - cy) + end + covXX = covXX / (#self.points - 1) + covYY = covYY / (#self.points - 1) + covXY = covXY / (#self.points - 1) + return covXX, covYY, covXY +end + +function POLYGON:Direction() + local covXX, covYY, covXY = self:CovarianceMatrix() + -- Simplified calculation for the largest eigenvector's direction + local theta = 0.5 * math.atan2(2 * covXY, covXX - covYY) + return math.cos(theta), math.sin(theta) +end + +--- Returns a random Vec2 within the polygon. The Vec2 is weighted by the areas of the triangles that make up the polygon. +-- @return #table The random Vec2 +function POLYGON:GetRandomVec2() + local weights = {} + for _, triangle in pairs(self.Triangles) do + weights[triangle] = triangle.SurfaceArea / self.SurfaceArea + end + + local random_weight = math.random() + local accumulated_weight = 0 + for triangle, weight in pairs(weights) do + accumulated_weight = accumulated_weight + weight + if accumulated_weight >= random_weight then + return triangle:GetRandomVec2() + end + end +end + +--- Returns a random non-weighted Vec2 within the polygon. The Vec2 is chosen from one of the triangles that make up the polygon. +-- @return #table The random non-weighted Vec2 +function POLYGON:GetRandomNonWeightedVec2() + return self.Triangles[math.random(1, #self.Triangles)]:GetRandomVec2() +end + +--- Checks if a point is contained within the polygon. The point is a table with 'x' and 'y' fields. +-- @param #table point The point to check +-- @param #table points (optional) Points of the polygon or other points if you're just using the POLYGON class without an object of it +-- @return #bool True if the point is contained, false otherwise +function POLYGON:ContainsPoint(point, polygon_points) + local x = point.x + local y = point.y + + polygon_points = polygon_points or self.Points + + local counter = 0 + local num_points = #polygon_points + for current_index = 1, num_points do + local next_index = (current_index % num_points) + 1 + local current_x, current_y = polygon_points[current_index].x, polygon_points[current_index].y + local next_x, next_y = polygon_points[next_index].x, polygon_points[next_index].y + if ((current_y > y) ~= (next_y > y)) and (x < (next_x - current_x) * (y - current_y) / (next_y - current_y) + current_x) then + counter = counter + 1 + end + end + return counter % 2 == 1 +end + +--- Draws the polygon on the map. The polygon can be drawn with or without inner triangles. This is just for debugging +-- @param #bool include_inner_triangles Whether to include inner triangles in the drawing +function POLYGON:Draw(include_inner_triangles) + include_inner_triangles = include_inner_triangles or false + for i=1, #self.Coords do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.OutlineMarkIDs, c1:LineToAll(c2)) + end + + + if include_inner_triangles then + for _, triangle in ipairs(self.Triangles) do + triangle:Draw() + end + end +end + +--- Removes the drawing of the polygon from the map. +function POLYGON:RemoveDraw() + for _, triangle in pairs(self.Triangles) do + triangle:RemoveDraw() + end + for _, mark_id in pairs(self.OutlineMarkIDs) do + UTILS.RemoveMark(mark_id) + end +end + +--- Calculates the surface area of the polygon. The surface area is the sum of the areas of the triangles that make up the polygon. +-- @return #number The surface area of the polygon +function POLYGON:__CalculateSurfaceArea() + local area = 0 + for _, triangle in pairs(self.Triangles) do + area = area + triangle.SurfaceArea + end + return area +end + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Moose Development/Moose/Shapes/ShapeBase.lua b/Moose Development/Moose/Shapes/ShapeBase.lua new file mode 100644 index 000000000..7042a44ad --- /dev/null +++ b/Moose Development/Moose/Shapes/ShapeBase.lua @@ -0,0 +1,216 @@ +--- **Shapes** - Class that serves as the base shapes drawn in the Mission Editor +-- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.SHAPE_BASE +-- @image CORE_Pathline.png + + +--- SHAPE_BASE class. +-- @type SHAPE_BASE +-- @field #string ClassName Name of the class. +-- @field #string Name Name of the shape +-- @field #table CenterVec2 Vec2 of the center of the shape, this will be assigned automatically +-- @field #table Points List of 3D points defining the shape, this will be assigned automatically +-- @field #table Coords List of COORDINATE defining the path, this will be assigned automatically +-- @field #table MarkIDs List any MARKIDs this class use, this will be assigned automatically +-- @extends Core.Base#BASE + +--- *I'm in love with the shape of you -- Ed Sheeran +-- +-- === +-- +-- # SHAPE_BASE +-- The class serves as the base class to deal with these shapes using MOOSE. You should never use this class on its own, +-- rather use: +-- CIRCLE +-- LINE +-- OVAL +-- POLYGON +-- TRIANGLE (although this one's a bit special as well) + +-- === +-- The idea is that anything you draw on the map in the Mission Editor can be turned in a shape to work with in MOOSE. +-- This is the base class that all other shape classes are built on. There are some shared functions, most of which are overridden in the derived classes + +-- @field #SHAPE_BASE + + +SHAPE_BASE = { + ClassName = "SHAPE_BASE", + Name = "", + CenterVec2 = nil, + Points = {}, + Coords = {}, + MarkIDs = {}, + ColorString = "", + ColorRGBA = {} +} + +--- Creates a new instance of SHAPE_BASE. +-- @return #SHAPE_BASE The new instance +function SHAPE_BASE:New() + local self = BASE:Inherit(self, BASE:New()) + return self +end + +--- Finds a shape on the map by its name. +-- @param #string shape_name Name of the shape to find +-- @return #SHAPE_BASE The found shape +function SHAPE_BASE:FindOnMap(shape_name) + local self = BASE:Inherit(self, BASE:New()) + + local found = false + + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if object["name"] == shape_name then + self.Name = object["name"] + self.CenterVec2 = { x = object["mapX"], y = object["mapY"] } + self.ColorString = object["colorString"] + self.ColorRGBA = UTILS.HexToRGBA(self.ColorString) + found = true + end + end + end + if not found then + self:E("Can't find a shape with name " .. shape_name) + end + return self +end + +function SHAPE_BASE:GetAllShapes(filter) + filter = filter or "" + local return_shapes = {} + for _, layer in pairs(env.mission.drawings.layers) do + for _, object in pairs(layer["objects"]) do + if string.contains(object["name"], filter) then + table.add(return_shapes, object) + end + end + end + + return return_shapes +end + +--- Offsets the shape to a new position. +-- @param #table new_vec2 The new position +function SHAPE_BASE:Offset(new_vec2) + local offset_vec2 = UTILS.Vec2Subtract(new_vec2, self.CenterVec2) + self.CenterVec2 = new_vec2 + if self.ClassName == "POLYGON" then + for _, point in pairs(self.Points) do + point.x = point.x + offset_vec2.x + point.y = point.y + offset_vec2.y + end + end +end + +--- Gets the name of the shape. +-- @return #string The name of the shape +function SHAPE_BASE:GetName() + return self.Name +end + +function SHAPE_BASE:GetColorString() + return self.ColorString +end + +function SHAPE_BASE:GetColorRGBA() + return self.ColorRGBA +end + +function SHAPE_BASE:GetColorRed() + return self.ColorRGBA.R +end + +function SHAPE_BASE:GetColorGreen() + return self.ColorRGBA.G +end + +function SHAPE_BASE:GetColorBlue() + return self.ColorRGBA.B +end + +function SHAPE_BASE:GetColorAlpha() + return self.ColorRGBA.A +end + +--- Gets the center position of the shape. +-- @return #table The center position +function SHAPE_BASE:GetCenterVec2() + return self.CenterVec2 +end + +--- Gets the center coordinate of the shape. +-- @return #COORDINATE The center coordinate +function SHAPE_BASE:GetCenterCoordinate() + return COORDINATE:NewFromVec2(self.CenterVec2) +end + +--- Gets the coordinate of the shape. +-- @return #COORDINATE The coordinate +function SHAPE_BASE:GetCoordinate() + return self:GetCenterCoordinate() +end + +--- Checks if a point is contained within the shape. +-- @param #table _ The point to check +-- @return #bool True if the point is contained, false otherwise +function SHAPE_BASE:ContainsPoint(_) + self:E("This needs to be set in the derived class") +end + +--- Checks if a unit is contained within the shape. +-- @param #string unit_name The name of the unit to check +-- @return #bool True if the unit is contained, false otherwise +function SHAPE_BASE:ContainsUnit(unit_name) + local unit = UNIT:FindByName(unit_name) + + if unit == nil or not unit:IsAlive() then + return false + end + + if self:ContainsPoint(unit:GetVec2()) then + return true + end + return false +end + +--- Checks if any unit of a group is contained within the shape. +-- @param #string group_name The name of the group to check +-- @return #bool True if any unit of the group is contained, false otherwise +function SHAPE_BASE:ContainsAnyOfGroup(group_name) + local group = GROUP:FindByName(group_name) + + if group == nil or not group:IsAlive() then + return false + end + + for _, unit in pairs(group:GetUnits()) do + if self:ContainsPoint(unit:GetVec2()) then + return true + end + end + return false +end + +--- Checks if all units of a group are contained within the shape. +-- @param #string group_name The name of the group to check +-- @return #bool True if all units of the group are contained, false otherwise +function SHAPE_BASE:ContainsAllOfGroup(group_name) + local group = GROUP:FindByName(group_name) + + if group == nil or not group:IsAlive() then + return false + end + + for _, unit in pairs(group:GetUnits()) do + if not self:ContainsPoint(unit:GetVec2()) then + return false + end + end + return true +end diff --git a/Moose Development/Moose/Shapes/Triangle.lua b/Moose Development/Moose/Shapes/Triangle.lua new file mode 100644 index 000000000..c60b2aeef --- /dev/null +++ b/Moose Development/Moose/Shapes/Triangle.lua @@ -0,0 +1,86 @@ +-- TRIANGLE class with properties and methods for handling triangles. This class is mostly used by the POLYGON class, but you can use it on its own as well +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- +TRIANGLE = { + ClassName = "TRIANGLE", + Points = {}, + Coords = {}, + SurfaceArea = 0 +} + +--- Creates a new triangle from three points. The points need to be given as Vec2s +-- @param #table p1 The first point of the triangle +-- @param #table p2 The second point of the triangle +-- @param #table p3 The third point of the triangle +-- @return #TRIANGLE The new triangle +function TRIANGLE:New(p1, p2, p3) + local self = BASE:Inherit(self, SHAPE_BASE:New()) + self.Points = {p1, p2, p3} + + local center_x = (p1.x + p2.x + p3.x) / 3 + local center_y = (p1.y + p2.y + p3.y) / 3 + self.CenterVec2 = {x=center_x, y=center_y} + + for _, pt in pairs({p1, p2, p3}) do + table.add(self.Coords, COORDINATE:NewFromVec2(pt)) + end + + self.SurfaceArea = math.abs((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y)) * 0.5 + + self.MarkIDs = {} + return self +end + +--- Checks if a point is contained within the triangle. +-- @param #table pt The point to check +-- @param #table points (optional) The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it +-- @return #bool True if the point is contained, false otherwise +function TRIANGLE:ContainsPoint(pt, points) + points = points or self.Points + + local function sign(p1, p2, p3) + return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y) + end + + local d1 = sign(pt, self.Points[1], self.Points[2]) + local d2 = sign(pt, self.Points[2], self.Points[3]) + local d3 = sign(pt, self.Points[3], self.Points[1]) + + local has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0) + local has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0) + + return not (has_neg and has_pos) +end + +--- Returns a random Vec2 within the triangle. +-- @param #table points The points of the triangle, or 3 other points if you're just using the TRIANGLE class without an object of it +-- @return #table The random Vec2 +function TRIANGLE:GetRandomVec2(points) + points = points or self.Points + local pt = {math.random(), math.random()} + table.sort(pt) + local s = pt[1] + local t = pt[2] - pt[1] + local u = 1 - pt[2] + + return {x = s * points[1].x + t * points[2].x + u * points[3].x, + y = s * points[1].y + t * points[2].y + u * points[3].y} +end + +--- Draws the triangle on the map, just for debugging +function TRIANGLE:Draw() + for i=1, #self.Coords do + local c1 = self.Coords[i] + local c2 = self.Coords[i % #self.Coords + 1] + table.add(self.MarkIDs, c1:LineToAll(c2)) + end +end + +--- Removes the drawing of the triangle from the map. +function TRIANGLE:RemoveDraw() + for _, mark_id in pairs(self.MarkIDs) do + UTILS.RemoveMark(mark_id) + end +end diff --git a/Moose Development/Moose/Utilities/Utils.lua b/Moose Development/Moose/Utilities/Utils.lua index 6bf7e1fc9..96cf8c1b4 100644 --- a/Moose Development/Moose/Utilities/Utils.lua +++ b/Moose Development/Moose/Utilities/Utils.lua @@ -3513,6 +3513,25 @@ function string.contains(str, value) return string.match(str, value) end + +--- Moves an object from one table to another +-- @param #obj object to move +-- @param #from_table table to move from +-- @param #to_table table to move to +function table.move_object(obj, from_table, to_table) + local index + for i, v in pairs(from_table) do + if v == obj then + index = i + end + end + + if index then + local moved = table.remove(from_table, index) + table.insert_unique(to_table, moved) + end +end + --- Given tbl is a indexed table ({"hello", "dcs", "world"}), checks if element exists in the table. --- The table can be made up out of complex tables or values as well -- @param #table tbl @@ -3731,6 +3750,25 @@ function UTILS.OctalToDecimal(Number) return tonumber(Number,8) end + +--- HexToRGBA +-- @param hex_string table +-- @return #table R, G, B, A +function UTILS.HexToRGBA(hex_string) + local hexNumber = tonumber(string.sub(hex_string, 3), 16) -- convert the string to a number + -- extract RGBA components + local alpha = hexNumber % 256 + hexNumber = (hexNumber - alpha) / 256 + local blue = hexNumber % 256 + hexNumber = (hexNumber - blue) / 256 + local green = hexNumber % 256 + hexNumber = (hexNumber - green) / 256 + local red = hexNumber % 256 + + return {R = red, G = green, B = blue, A = alpha} +end + + --- Function to save the position of a set of #OPSGROUP (ARMYGROUP) objects. -- @param Core.Set#SET_OPSGROUP Set of ops objects to save -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. @@ -3768,7 +3806,7 @@ function UTILS.SaveSetOfOpsGroups(Set,Path,Filename,Structured) data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d,%s\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z,strucdata) else data = string.format("%s%s,%s,%s,%s,%d,%d,%d,%d\n",data,name,legion,template,alttemplate,units,position.x,position.y,position.z) - end + end end end -- save the data @@ -3780,12 +3818,12 @@ end -- @param #string Path The path to use. Use double backslashes \\\\ on Windows filesystems. -- @param #string Filename The name of the file. -- @return #table Returns a table of data entries: `{ groupname=groupname, size=size, coordinate=coordinate, template=template, structure=structure, legion=legion, alttemplate=alttemplate }` --- Returns nil when the file cannot be read. +-- Returns nil when the file cannot be read. function UTILS.LoadSetOfOpsGroups(Path,Filename) local filename = Filename or "SetOfGroups" local datatable = {} - + if UTILS.CheckFileExists(Path,filename) then local outcome,loadeddata = UTILS.LoadFromFile(Path,Filename) -- remove header @@ -3820,20 +3858,20 @@ end -- @param #number tgtHdg The absolute heading from the reference object to the target object/point in 0-360 -- @return #string text Text in clock heading such as "4 O'CLOCK" -- @usage Display the range and clock distance of a BTR in relation to REAPER 1-1's heading: --- +-- -- myUnit = UNIT:FindByName( "REAPER 1-1" ) -- myTarget = GROUP:FindByName( "BTR-1" ) --- +-- -- coordUnit = myUnit:GetCoordinate() -- coordTarget = myTarget:GetCoordinate() --- +-- -- hdgUnit = myUnit:GetHeading() -- hdgTarget = coordUnit:HeadingTo( coordTarget ) -- distTarget = coordUnit:Get3DDistance( coordTarget ) --- +-- -- clockString = UTILS.ClockHeadingString( hdgUnit, hdgTarget ) --- --- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! +-- +-- -- Will show this message to REAPER 1-1 in-game: Contact BTR at 3 o'clock for 1134m! -- MESSAGE:New("Contact BTR at " .. clockString .. " for " .. distTarget .. "m!):ToUnit( myUnit ) function UTILS.ClockHeadingString(refHdg,tgtHdg) local relativeAngle = tgtHdg - refHdg From bc5946c76efacf44a8a539b18a3f32b534183b8e Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Tue, 23 Apr 2024 09:25:47 +0200 Subject: [PATCH 14/15] #SHAPES a bit of extra docu --- Moose Development/Moose/Shapes/Circle.lua | 6 +++--- Moose Development/Moose/Shapes/Cube.lua | 18 ++++++++++++++++++ Moose Development/Moose/Shapes/Line.lua | 7 ++++--- Moose Development/Moose/Shapes/Oval.lua | 9 ++++----- Moose Development/Moose/Shapes/Polygon.lua | 9 ++++----- Moose Development/Moose/Shapes/ShapeBase.lua | 6 ++---- Moose Development/Moose/Shapes/Triangle.lua | 16 +++++++++++++++- 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Moose Development/Moose/Shapes/Circle.lua b/Moose Development/Moose/Shapes/Circle.lua index 04c153d86..3c4efc10c 100644 --- a/Moose Development/Moose/Shapes/Circle.lua +++ b/Moose Development/Moose/Shapes/Circle.lua @@ -17,12 +17,12 @@ -- # CIRCLE -- CIRCLEs can be fetched from the drawings in the Mission Editor +--- -- This class has some of the standard CIRCLE functions you'd expect. One function of interest is CIRCLE:PointInSector() that you can use if a point is -- within a certain sector (pizza slice) of a circle. This can be useful for many things, including rudimentary, "radar-like" searches from a unit. - +-- +-- CIRCLE class with properties and methods for handling circles. -- @field #CIRCLE - ---- CIRCLE class with properties and methods for handling circles. CIRCLE = { ClassName = "CIRCLE", Radius = nil, diff --git a/Moose Development/Moose/Shapes/Cube.lua b/Moose Development/Moose/Shapes/Cube.lua index ae3f73090..18448fe85 100644 --- a/Moose Development/Moose/Shapes/Cube.lua +++ b/Moose Development/Moose/Shapes/Cube.lua @@ -1,3 +1,21 @@ +--- +-- +-- ### Author: **nielsvaes/coconutcockpit** +-- +-- === +-- @module Shapes.CUBE + +--- LINE class. +-- @type CUBE +-- @field #string ClassName Name of the class. +-- @field #number Points points of the line +-- @field #number Coords coordinates of the line + +-- +-- === + +--- +-- @field #CUBE CUBE = { ClassName = "CUBE", Points = {}, diff --git a/Moose Development/Moose/Shapes/Line.lua b/Moose Development/Moose/Shapes/Line.lua index 08f7c84a0..9b860000d 100644 --- a/Moose Development/Moose/Shapes/Line.lua +++ b/Moose Development/Moose/Shapes/Line.lua @@ -1,12 +1,12 @@ --- +--- -- -- ### Author: **nielsvaes/coconutcockpit** -- -- === -- @module Shapes.LINE ---- OVAL class. --- @type OVAL +--- LINE class. +-- @type LINE -- @field #string ClassName Name of the class. -- @field #number Points points of the line -- @field #number Coords coordinates of the line @@ -14,6 +14,7 @@ -- -- === +--- -- @field #LINE LINE = { ClassName = "LINE", diff --git a/Moose Development/Moose/Shapes/Oval.lua b/Moose Development/Moose/Shapes/Oval.lua index d2f85a822..d1a65b58d 100644 --- a/Moose Development/Moose/Shapes/Oval.lua +++ b/Moose Development/Moose/Shapes/Oval.lua @@ -1,4 +1,4 @@ --- +--- -- -- ### Author: **nielsvaes/coconutcockpit** -- @@ -18,18 +18,17 @@ -- -- # OVAL -- OVALs can be fetched from the drawings in the Mission Editor - +-- -- The major and minor axes define how elongated the shape of an oval is. This class has some basic functions that the other SHAPE classes have as well. -- Since it's not possible to draw the shape of an oval while the mission is running, right now the draw function draws 2 cicles. One with the major axis and one with -- the minor axis. It then draws a diamond shape on an angle where the corners touch the major and minor axes to give an indication of what the oval actually -- looks like. - +-- -- Using ovals can be handy to find an area on the ground that is actually an intersection of a cone and a plane. So imagine you're faking the view cone of -- a targeting pod and --- @field #OVAL - --- OVAL class with properties and methods for handling ovals. +-- @field #OVAL OVAL = { ClassName = "OVAL", MajorAxis = nil, diff --git a/Moose Development/Moose/Shapes/Polygon.lua b/Moose Development/Moose/Shapes/Polygon.lua index a40256ecf..0d0707570 100644 --- a/Moose Development/Moose/Shapes/Polygon.lua +++ b/Moose Development/Moose/Shapes/Polygon.lua @@ -1,4 +1,4 @@ --- +--- -- -- ### Author: **nielsvaes/coconutcockpit** -- @@ -26,10 +26,10 @@ -- * A rect -- Use the POLYGON:FindOnMap() of POLYGON:Find() functions for this. You can also create a non existing polygon in memory using the POLYGON:New() function. Pass in a -- any number of Vec2s into this function to define the shape of the polygon you want. - +-- -- You can draw very intricate and complex polygons in the Mission Editor to avoid (or include) map objects. You can then generate random points within this complex -- shape for spawning groups or checking positions. - +-- -- When a POLYGON is made, it's automatically triangulated. The resulting triangles are stored in POLYGON.Triangles. This also immeadiately saves the surface area -- of the POLYGON. Because the POLYGON is triangulated, it's possible to generate random points within this POLYGON without having to use a trial and error method to see if -- the point is contained within the shape. @@ -37,9 +37,8 @@ -- which ignores the size of the triangles in the polygon to pick a random points. This will result in more points clumping together in parts of the polygon where the triangles are -- the smallest. - +--- -- @field #POLYGON - POLYGON = { ClassName = "POLYGON", Points = {}, diff --git a/Moose Development/Moose/Shapes/ShapeBase.lua b/Moose Development/Moose/Shapes/ShapeBase.lua index 7042a44ad..fdf8515c2 100644 --- a/Moose Development/Moose/Shapes/ShapeBase.lua +++ b/Moose Development/Moose/Shapes/ShapeBase.lua @@ -30,14 +30,12 @@ -- OVAL -- POLYGON -- TRIANGLE (although this one's a bit special as well) - +-- -- === -- The idea is that anything you draw on the map in the Mission Editor can be turned in a shape to work with in MOOSE. -- This is the base class that all other shape classes are built on. There are some shared functions, most of which are overridden in the derived classes - +-- -- @field #SHAPE_BASE - - SHAPE_BASE = { ClassName = "SHAPE_BASE", Name = "", diff --git a/Moose Development/Moose/Shapes/Triangle.lua b/Moose Development/Moose/Shapes/Triangle.lua index c60b2aeef..e0e752ce8 100644 --- a/Moose Development/Moose/Shapes/Triangle.lua +++ b/Moose Development/Moose/Shapes/Triangle.lua @@ -1,8 +1,22 @@ --- TRIANGLE class with properties and methods for handling triangles. This class is mostly used by the POLYGON class, but you can use it on its own as well +--- TRIANGLE class with properties and methods for handling triangles. This class is mostly used by the POLYGON class, but you can use it on its own as well -- -- ### Author: **nielsvaes/coconutcockpit** -- -- +-- === +-- @module Shapes.TRIANGLE + +--- LINE class. +-- @type CUBE +-- @field #string ClassName Name of the class. +-- @field #number Points points of the line +-- @field #number Coords coordinates of the line + +-- +-- === + +--- +-- @field #TRIANGLE TRIANGLE = { ClassName = "TRIANGLE", Points = {}, From 2220f1829fd3fa5a70f5d2a8982cbe4712435249 Mon Sep 17 00:00:00 2001 From: Applevangelist Date: Tue, 23 Apr 2024 10:13:09 +0200 Subject: [PATCH 15/15] #STRATEGO -- add SetStrategoZone --- .../Moose/Functional/Stratego.lua | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Moose Development/Moose/Functional/Stratego.lua b/Moose Development/Moose/Functional/Stratego.lua index 97bf316f5..07bac3837 100644 --- a/Moose Development/Moose/Functional/Stratego.lua +++ b/Moose Development/Moose/Functional/Stratego.lua @@ -15,6 +15,7 @@ -- -- @module Functional.Stratego -- @image Functional.Stratego.png +-- Last Update April 2024 --- @@ -42,6 +43,7 @@ -- @field #number CaptureUnits -- @field #number CaptureThreatlevel -- @field #boolean ExcludeShips +-- @field Core.Zone#ZONE StrategoZone -- @extends Core.Base#BASE -- @extends Core.Fsm#FSM @@ -154,6 +156,7 @@ -- @{#STRATEGO.FindRoute}(): Find a route between two nodes. -- @{#STRATEGO.SetCaptureOptions}(): Set how many units of which minimum threat level are needed to capture one node (i.e. the underlying OpsZone). -- @{#STRATEGO.SetDebug}(): Set debug and draw options. +-- @{#STRATEGO.SetStrategoZone}(): Set a zone to restrict STRATEGO analytics to, can be any kind of ZONE Object. -- -- -- ## Visualisation example code for the Syria map: @@ -177,7 +180,7 @@ STRATEGO = { debug = false, drawzone = false, markzone = false, - version = "0.2.6", + version = "0.2.7", portweight = 3, POIweight = 1, maxrunways = 3, @@ -377,6 +380,15 @@ function STRATEGO:SetDebug(Debug,DrawZones,MarkZones) return self end +--- [USER] Restrict Stratego to analyse this zone only. +-- @param #STRATEGO self +-- @param Core.Zone#ZONE Zone The Zone to restrict Stratego to, can be any kind of ZONE Object. +-- @return #STRATEGO self +function STRATEGO:SetStrategoZone(Zone) + self.StrategoZone = Zone + return self +end + --- [USER] Set weights for nodes and routes to determine their importance. -- @param #STRATEGO self -- @param #number MaxRunways Set the maximum number of runways the big (equals strategic) airbases on the map have. Defaults to 3. The weight of an airbase node hence equals the number of runways. @@ -425,12 +437,19 @@ function STRATEGO:AnalyseBases() local airbasetable = self.airbasetable local nonconnectedab = self.nonconnectedab local easynames = self.easynames + local zone = self.StrategoZone -- Core.Zone#ZONE_POLYGON -- find bases with >= 1 runways self.bases:ForEach( function(afb) local ab = afb -- Wrapper.Airbase#AIRBASE + local abvec2 = ab:GetVec2() if self.ExcludeShips and ab:IsShip() then return end + if zone ~= nil then + if not zone:IsVec2InZone(abvec2) then + return + end + end local abname = ab:GetName() local runways = ab:GetRunways() local numrwys = #runways