Quaggles / dcs-lua-datamine

A reference guide to the lua table values in DCS for weapons and aircraft
103 stars 21 forks source link

Help with generating Aircraft based weapon lists #12

Open ozdeadmeat opened 3 months ago

ozdeadmeat commented 3 months ago

I am wondering if there is a programmatic way to generate a list of weapon types for each playable Aircraft?

This is primarily so I can add specific aircraft to a FARP object and make sure that only the specific weapons for that aircraft are present.

Quaggles commented 3 months ago

You can write a hooks script like this repository uses to export but tailored to export only what you need, here is an example script I've used previously to export the IR and RCS values for all the aircraft.

local dumpFolder = "DCS.Lua.Exporter\\" --Relative to lfs.writedir, aka "Saved Games\DCS\"
local function ExportAircraftProperties()
    local absoluteDirectory = lfs.writedir()..dumpFolder..""
    -- Creates the dumpFolder if it doesn't exist
    if not lfs.attributes(absoluteDirectory) then
        lfs.mkdir(absoluteDirectory)
    end

    local filepath = absoluteDirectory.."/Aircraft-RCS-IR-Properties.csv"
    local file, error = io.open (filepath, "w")
    if error then
        log.write("DCS.Lua.Exporter", log.ERROR, "Error writing to disk "..error)
    else
        for _, unit in ipairs(db.Units.Planes.Plane) do
            local rcs = unit["RCS"]
            local ir = unit["IR_emission_coeff"]
            local ir_ab = unit["IR_emission_coeff_ab"]
            -- If aircraft has no afterburner clear ir_ab
            if ir == ir_ab or ir_ab == 0 then
                ir_ab=''
            end
            file:write('Airplane,'..unit["DisplayName"] ..','.. rcs ..','.. ir ..','.. ir_ab ..'\n')
        end
        for _, unit in pairs(db.Units.Helicopters.Helicopter) do
            local rcs = unit["RCS"]
            local ir = unit["IR_emission_coeff"]
            file:write('Helicopter,'..unit["DisplayName"] ..','.. rcs ..','.. ir ..'\n')
        end
    end
    file:close()
end

local status, error = pcall(ExportAircraftProperties)
if error then
    log.write("DCS.Lua.Exporter", log.ERROR, "Error in ExportAircraftProperties: "..error)
end

Save it as "DCS World/Scripts/Hooks/DCS_IR_RCS_Export.lua" and it will be executed every time DCS runs and create a csv table at "~\Saved Games\DCS\DCS.Lua.Exporter\Aircraft-RCS-IR-Properties.csv".

The script already loops through all the planes and helicopters you should just need to modify it to loop through each aircrafts Pylons table and grab the CLSID for all launchers that can be mounted on the aircraft, eg: https://github.com/Quaggles/dcs-lua-datamine/blob/master/_G/db/Units/Planes/Plane/A-10A.lua#L658

Then you should be able to use that CLSID to lookup the actual launcher definition like so _G.launcher["{CBU-87}"] then you can get the launchers attribute table to put in the warehouses file: https://github.com/Quaggles/dcs-lua-datamine/blob/master/_G/launcher/%7BCBU-87%7D.lua#L9

(In the datamine on github the last element of the attributes/wstype is redacted but when you grab via your it you'll get the actual numbers) such as

["wsType"] = 
{
    [1] = 1,
    [2] = 3,
    [3] = 43,
    [4] = 616,
}, -- end of ["wsType"]
Jeandcc commented 1 week ago

@Quaggles , this is great info for a project of mine as well. Could you let me know where you found documentation regarding the db.Units.Planes.Plane variable?

I didn't see mentions of it anywhere in the /API/DCS_ControlAPI.html file, and my inexperienced backgorund in lua doesn't provide me with the means of finding out what is available in that runtime. (I'm well versed with TypeScript for context). Did you just drop a bunch of log statements to find out the variables available in that environment, or did you you use documentation to guide your usage of "DCS variables" inside the hooks scripts folder?

Quaggles commented 1 week ago

Very little in DCS is documented unfortunately, I basically just loaded up a connection to the GUI environment (hooks folder) and then used https://github.com/kikito/inspect.lua to browse around what looked interesting. You can use https://dcsfiddle.pages.dev/, certain parts of the environment aren't available from the Saved Games hooks folder instead install dcs-fiddle-server.lua into 'DCS World/Scripts/Hooks'. Setup docs here. Make sure to pick GUI environment at the top. Once you get that setup run a script like this browse around:

-- Put inspect.lua in 'DCS World/Scripts' for this to work
local inspect = require 'inspect'

-- Use this to export the lua tables in a human readable format
return inspect(_G.weapons_table.weapons)

-- Use this to prevent inspect from getting child tables recursively in case you are previewing a high level table
-- return inspect(_G.weapons_table.weapons, {depth = 1})

DCS Fiddle has an "Explorer" function for browsing the environment but for some tables it throws an error when you try to open them, inspect.lua seems to handle pretty much everything (And it's what the dataminer uses to get the human readable files)

Jeandcc commented 5 days ago

Thanks @Quaggles ! This is already super helpful, and it definitely allowed me to do what I needed: Compile the list of all Planes in DCS, their weapons and their technical specs (Type, Range, etc), and export it out as a JSON. I'll just build a small UI piece for me and I'll have my own qrh app connected to DCSs actual data. I'll make sure to send a shoutout to you whenever I get it live.

Thanks a lot!