Initiate facility in line

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
Parel803
Posts: 932
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Initiate facility in line

Post by Parel803 »

Good afternoon,

Based on the video from P.Gitcomb, appriciated as always, and time to see what can be done. Were I got help from my AI. I tried to make the same bridge (and pipeline) with flex in directions.

Code: Select all

-- Bridge xx degrees bearing, length in Nm and spacing check in size of unit line icons

local side = "Red"
local unit_name_prefix = "Pipeline Section"
local unit_type = "Facility"
local dbid = 4227
local start_lat = 52.5062383597972  -- Starting latitude. ctrl+x give position from game (check , and .)
local start_lon = 1.76308848215427  -- Starting longitude
local spacing_meters = 40  -- Spacing in meters based on size of unit
local bearing = 123  -- Bearing in degrees (e.g., 0 for north, 90 for east)
local total_distance_nm = 86  -- Total distance in nautical miles

-- Convert total distance from nautical miles to meters
local total_distance_meters = total_distance_nm * 1852

-- Calculate the number of units, 
local num_units = math.ceil(total_distance_meters / spacing_meters) --  math.ceil rounds to the nearest integer.
-- local num_units = math.floor(total_distance_meters / spacing_meters)  -- rounding down units

-- Convert bearing to radians
local bearing_rad = math.rad(bearing)

-- Convert spacing to degrees
local spacing_lat = spacing_meters / 111320  -- Approximate conversion at the equator
local spacing_lon = spacing_lat / math.cos(math.rad(start_lat))

-- Calculate the change in latitude and longitude.
local delta_lat = spacing_lat * math.cos(bearing_rad)
local delta_lon = spacing_lon * math.sin(bearing_rad)

for i = 0, num_units - 1 do
    local unit_name = unit_name_prefix .. (i + 1)
    local lat = start_lat + (i * delta_lat)
    local lon = start_lon + (i * delta_lon)
    ScenEdit_AddUnit({
        type = unit_type,
        unitname = unit_name,
        dbid = dbid,
        side = side,
        latitude = lat,
        longitude = lon,
    })
end
It looks like the 'customizing icons' is not yet a feature. At least I have it greyed out. My thought was to make a small, maybe specific colour if possible, and have it less noise on my main screen. As an alternative for a drawing tool.

From there I try to make something with geopos.

Code: Select all

-- Bridge between waypoints

-- Bridge sections between three fixed points
local side = "Red"
local unit_name_prefix = "Bridge Section"
local unit_type = "Facility"
local dbid = 4126
local spacing_meters = 40  -- Spacing in meters based on size of unit

-- Define waypoints, for the number of you require
local waypoints = {
    {lat = 51.8315484366369, lon = 4.04777010189637},
    {lat = 51.837937738931, lon = 4.05059163834661},
    {lat = 51.8409613623701, lon = 4.06044655771694}
}

-- Function to calculate distance between two points in meters
function calculate_distance(lat1, lon1, lat2, lon2)
    local R = 6371000  -- Earth's radius in meters
    local lat1_rad = math.rad(lat1)
    local lat2_rad = math.rad(lat2)
    local delta_lat = math.rad(lat2 - lat1)
    local delta_lon = math.rad(lon2 - lon1)

    local a = math.sin(delta_lat/2) * math.sin(delta_lat/2) +
              math.cos(lat1_rad) * math.cos(lat2_rad) *
              math.sin(delta_lon/2) * math.sin(delta_lon/2)
    local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
    return R * c
end

-- Function to calculate bearing between two points
function calculate_bearing(lat1, lon1, lat2, lon2)
    local lat1_rad = math.rad(lat1)
    local lat2_rad = math.rad(lat2)
    local delta_lon = math.rad(lon2 - lon1)

    local y = math.sin(delta_lon) * math.cos(lat2_rad)
    local x = math.cos(lat1_rad) * math.sin(lat2_rad) -
              math.sin(lat1_rad) * math.cos(lat2_rad) * math.cos(delta_lon)
    local bearing_rad = math.atan2(y, x)
    return (math.deg(bearing_rad) + 360) % 360
end

-- Function to calculate new point given start point, bearing and distance
function calculate_new_point(lat, lon, bearing, distance)
    local R = 6371000  -- Earth's radius in meters
    local bearing_rad = math.rad(bearing)
    local lat_rad = math.rad(lat)
    
    local distance_rad = distance / R
    
    local new_lat_rad = math.asin(
        math.sin(lat_rad) * math.cos(distance_rad) +
        math.cos(lat_rad) * math.sin(distance_rad) * math.cos(bearing_rad)
    )
    
    local new_lon_rad = math.rad(lon) + math.atan2(
        math.sin(bearing_rad) * math.sin(distance_rad) * math.cos(lat_rad),
        math.cos(distance_rad) - math.sin(lat_rad) * math.sin(new_lat_rad)
    )
    
    return math.deg(new_lat_rad), math.deg(new_lon_rad)
end

-- Initialize unit counter
local unit_counter = 1

-- Process each segment between waypoints
for i = 1, #waypoints - 1 do
    local start_point = waypoints[i]
    local end_point = waypoints[i + 1]
    
    -- Calculate distance and bearing for this segment
    local segment_distance = calculate_distance(
        start_point.lat, start_point.lon,
        end_point.lat, end_point.lon
    )
    local bearing = calculate_bearing(
        start_point.lat, start_point.lon,
        end_point.lat, end_point.lon
    )
    
    -- Calculate number of units for this segment
    local num_units = math.ceil(segment_distance / spacing_meters)
    
    -- Place units along the segment
    for j = 0, num_units - 1 do
        local current_distance = j * spacing_meters
        local lat, lon = calculate_new_point(
            start_point.lat, start_point.lon,
            bearing, current_distance
        )
        
        -- Create the unit
        ScenEdit_AddUnit({
            type = unit_type,
            unitname = unit_name_prefix .. unit_counter,
            dbid = dbid,
            side = side,
            latitude = lat,
            longitude = lon,
        })
        
        unit_counter = unit_counter + 1
    end
end
It is a hobby, I'm no coder so let me know it it can be done better or without errors

regards GJ
Post Reply

Return to “Lua Legion”