From 6adef473400b85ecf0ffa7f84e9da8a5b495c075 Mon Sep 17 00:00:00 2001 From: Frank Date: Tue, 7 Feb 2023 22:47:55 +0100 Subject: [PATCH] PATHLINE Added new class PATHLINE --- Moose Development/Moose/Core/Database.lua | 75 +++++++++- Moose Development/Moose/Core/Pathline.lua | 163 ++++++++++++++++++++++ Moose Development/Moose/Modules.lua | 1 + Moose Setup/Moose.files | 1 + 4 files changed, 234 insertions(+), 6 deletions(-) create mode 100644 Moose Development/Moose/Core/Pathline.lua diff --git a/Moose Development/Moose/Core/Database.lua b/Moose Development/Moose/Core/Database.lua index 2bd712f72..8cfad8cd2 100644 --- a/Moose Development/Moose/Core/Database.lua +++ b/Moose Development/Moose/Core/Database.lua @@ -88,6 +88,7 @@ DATABASE = { WAREHOUSES = {}, FLIGHTGROUPS = {}, FLIGHTCONTROLS = {}, + PATHLINES = {}, } local _DATABASECoalition = @@ -244,7 +245,7 @@ function DATABASE:FindAirbase( AirbaseName ) end -do -- Zones +do -- Zones and Pathlines --- Finds a @{Core.Zone} based on the zone name. -- @param #DATABASE self @@ -266,8 +267,7 @@ do -- Zones self.ZONES[ZoneName] = Zone end end - - + --- Deletes a @{Core.Zone} from the DATABASE based on the zone name. -- @param #DATABASE self -- @param #string ZoneName The name of the zone. @@ -277,6 +277,39 @@ do -- Zones end + --- Adds a @{Core.Pathline} based on its name in the DATABASE. + -- @param #DATABASE self + -- @param #string PathlineName The name of the pathline + -- @param Core.Pathline#PATHLINE Pathline The pathline. + function DATABASE:AddPathline( PathlineName, Pathline ) + + if not self.PATHLINES[PathlineName] then + self.PATHLINES[PathlineName]=Pathline + end + end + + --- Finds a @{Core.Pathline} by its name. + -- @param #DATABASE self + -- @param #string PathlineName The name of the Pathline. + -- @return Core.Pathline#PATHLINE The found PATHLINE. + function DATABASE:FindPathline( PathlineName ) + + local pathline = self.PATHLINES[PathlineName] + + return pathline + end + + + --- Deletes a @{Core.Pathline} from the DATABASE based on its name. + -- @param #DATABASE self + -- @param #string PathlineName The name of the PATHLINE. + function DATABASE:DeletePathline( PathlineName ) + + self.PATHLINES[PathlineName]=nil + + return self + end + --- Private method that registers new ZONE_BASE derived objects within the DATABASE Object. -- @param #DATABASE self -- @return #DATABASE self @@ -383,7 +416,7 @@ do -- Zones for objectID, objectData in pairs(layerData.objects or {}) do -- Check for polygon which has at least 4 points (we would need 3 but the origin seems to be there twice) - if objectData.polygonMode=="free" and objectData.points and #objectData.points>=4 then + if objectData.polygonMode and objectData.polygonMode=="free" and objectData.points and #objectData.points>=4 then -- Name of the zone. local ZoneName=objectData.name or "Unknown Drawing" @@ -409,7 +442,7 @@ do -- Zones table.remove(points, #points) -- Debug output - self:I(string.format("Register ZONE: %s (Polygon drawing with %d verticies)", ZoneName, #points)) + self:I(string.format("Register ZONE: %s (Polygon drawing with %d vertices)", ZoneName, #points)) -- Create new polygon zone. local Zone=ZONE_POLYGON:NewFromPointsArray(ZoneName, points) @@ -421,8 +454,38 @@ do -- Zones self.ZONENAMES[ZoneName] = ZoneName -- Add zone. - self:AddZone(ZoneName, Zone) + self:AddZone(ZoneName, Zone) + elseif objectData.lineMode and objectData.lineMode=="segments" and objectData.points and #objectData.points>=2 then + + -- Name of the zone. + local Name=objectData.name or "Unknown Line Drawing" + + -- Reference point. All other points need to be translated by this. + local vec2={x=objectData.mapX, y=objectData.mapY} + + -- Copy points array. + local points=UTILS.DeepCopy(objectData.points) + + -- Translate points. + for i,_point in pairs(points) do + local point=_point --DCS#Vec2 + points[i]=UTILS.Vec2Add(point, vec2) + end + + + -- Debug output + self:I(string.format("Register PATHLINE: %s (Line drawing with %d points)", Name, #points)) + + -- Create new polygon zone. + local Pathline=PATHLINE:NewFromVec2Array(Name, points) + + -- Set color. + --Zone:SetColor({1, 0, 0}, 0.15) + + -- Add zone. + self:AddPathline(Name,Pathline) + end end diff --git a/Moose Development/Moose/Core/Pathline.lua b/Moose Development/Moose/Core/Pathline.lua new file mode 100644 index 000000000..058933107 --- /dev/null +++ b/Moose Development/Moose/Core/Pathline.lua @@ -0,0 +1,163 @@ +--- **Core** - Path from A to B. +-- +-- **Main Features:** +-- +-- * Path from A to B +-- * Arbitrary number of segments +-- * Automatically from lines drawtool +-- +-- === +-- +-- ### Author: **funkyfranky** +-- +-- === +-- @module Core.Pathline +-- @image CORE_Pathline.png + + +--- PATHLINE class. +-- @type PATHLINE +-- @field #string ClassName Name of the class. +-- @field #string lid Class id string for output to DCS log file. +-- @field #string name Name of the path line. +-- @field #table points List of 3D points defining the path. +-- @extends Core.Base#BASE + +--- *When nothing goes right... Go left!* +-- +-- === +-- +-- # The PATHLINE Concept +-- +-- List of points defining a path from A to B. +-- +-- Line drawings created in the mission editor are automatically registered as pathlines and stored in the MOOSE database. +-- They can be accessed with the @{#PATHLINE.FindByName) function. +-- +-- +-- @field #PATHLINE +PATHLINE = { + ClassName = "PATHLINE", + lid = nil, + points = {}, +} + + +--- PATHLINE class version. +-- @field #string version +PATHLINE.version="0.0.1" + +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +-- TODO list +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +-- TODO: A lot... + +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +-- Constructor +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +--- Create a new PATHLINE object. Points need to be added later. +-- @param #PATHLINE self +-- @param #string Name Name of the path. +-- @return #PATHLINE self +function PATHLINE:New(Name) + + -- Inherit everything from INTEL class. + local self=BASE:Inherit(self, BASE:New()) --#PATHLINE + + self.name=Name or "Unknown Path" + + self.lid=string.format("PATHLINE %s | ", Name) + + return self +end + +--- Create a new PATHLINE object from a given list of 2D points. +-- @param #PATHLINE self +-- @param #string Name Name of the pathline. +-- @param #table Vec2Array List of DCS#Vec2 points. +-- @return #PATHLINE self +function PATHLINE:NewFromVec2Array(Name, Vec2Array) + + local self=PATHLINE:New(Name) + + for i=1,#Vec2Array do + self:AddPointFromVec2(Vec2Array[i]) + end + + return self +end + +--- Create a new PATHLINE object from a given list of 3D points. +-- @param #PATHLINE self +-- @param #string Name Name of the pathline. +-- @param #table Vec3Array List of DCS#Vec3 points. +-- @return #PATHLINE self +function PATHLINE:NewFromVec3Array(Name, Vec3Array) + + local self=PATHLINE:New(Name) + + for i=1,#Vec3Array do + self:AddPointFromVec3(Vec3Array[i]) + end + + return self +end + +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +-- User functions +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +--- Find a pathline in the database. +-- @param #PATHLINE self +-- @param #string Name The name of the pathline. +-- @return #PATHLINE self +function PATHLINE:FindByName(Name) + local pathline = _DATABASE:FindPathline(Name) + return pathline +end + +--- Add a 2D point to the path. The third dimension is determined from the land height. +-- @param #PATHLINE self +-- @param DCS#Vec2 Vec2 The 2D vector (x,y) to add. +-- @return #PATHLINE self +function PATHLINE:AddPointFromVec2(Vec2) + + if Vec2 then + + local Vec3={x=Vec2.x, y=land.getHeight(Vec2), z=Vec2.y} + + self:AddPointFromVec3(Vec3) + + end + + return self +end + +--- Add a 3D point to the path. +-- @param #PATHLINE self +-- @param DCS#Vec3 Vec3 The §D vector (x,y) to add. +-- @return #PATHLINE self +function PATHLINE:AddPointFromVec3(Vec3) + + if Vec3 then + + table.insert(self.points, Vec3) + + end + + return self +end + +--- Get 3D points of pathline. +-- @param #PATHLINE self +-- @return List of DCS#Vec3 points. +function PATHLINE:GetPoints3D() + return self.points +end + + +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/Moose Development/Moose/Modules.lua b/Moose Development/Moose/Modules.lua index c0ed4dd90..eda4bd2b2 100644 --- a/Moose Development/Moose/Modules.lua +++ b/Moose Development/Moose/Modules.lua @@ -33,6 +33,7 @@ __Moose.Include( 'Scripts/Moose/Core/Goal.lua' ) __Moose.Include( 'Scripts/Moose/Core/Spot.lua' ) __Moose.Include( 'Scripts/Moose/Core/MarkerOps_Base.lua' ) __Moose.Include( 'Scripts/Moose/Core/TextAndSound.lua' ) +__Moose.Include( 'Scripts/Moose/Core/Pathline.lua' ) __Moose.Include( 'Scripts/Moose/Wrapper/Object.lua' ) __Moose.Include( 'Scripts/Moose/Wrapper/Identifiable.lua' ) diff --git a/Moose Setup/Moose.files b/Moose Setup/Moose.files index 9531ed658..dbc0c37e1 100644 --- a/Moose Setup/Moose.files +++ b/Moose Setup/Moose.files @@ -34,6 +34,7 @@ Core/MarkerOps_Base.lua Core/Astar.lua Core/Condition.lua Core/TextAndSound.lua +Core/Pathline.lua Wrapper/Object.lua Wrapper/Identifiable.lua