DCS task attackUnit

From DCS World Wiki - Hoggitworld.com


Task: AttackUnit Added with: 1.2.4
Hierarchy: Scripting Root > Tasks > Main task > AttackUnit
Tasking Type: Main task
For: Airplanes, Helicopters
Available Under:
Description: Assigns the controlled group to attack a specified unit.

Note: The targeted unit becomes automatically detected for the controlled group.

Format: This task needs to be in the following format.
 AttackUnit = { 
  id = 'AttackUnit', 
  params = { 
    unitId = Unit.ID, 
    weaponType = number, 
    expend = enum AI.Task.WeaponExpend
    direction = Azimuth, 
    attackQtyLimit = boolean, 
    attackQty = number, 
    groupAttack = boolean, 
 } 
}

Required parameters: unitId

Optional Parameters: All other variables are optional. Some variables require other entries, for example the boolean values require the corresponding value if the boolean is true.

expend: Enum that defines how much munitions the AI will expend per attack run.

attackQty: Number of times the group will attack if the target is still alive and AI still have ammo. attackQtyLimit must be true.

groupAttack: If true then each aircraft in the group will attack the unit. By default only a single aircraft will attack each unit. Useful for attacking ships like the Moscow where you must literally shoot 20 missiles at it in order to beat its defenses.

Enumerators: weaponType is a number associated with a correspond weapons flags

AI.Task.WeaponExpend

"QUARTER" = "Quarter"
"TWO"     = "Two"
"ONE"     = "One"
"FOUR"    = "Four"
"HALF"    = "Half"
"ALL"     = "All"
Examples: The following is a function that is passed a flight name and a target group name. It searches the target group for units with SAM SR and SAM TR attributes (search radar and tracking radar). It then assigns an attack unit task to the flight for each of those units. Allows the flight to make a single attack on the target firing any two air to surface missiles at each target.
function smartAttackSam(flight, target)
    local tgtGP = Group.getByName(target)
    local fGP = Group.getByName(flight)
    local attribs = {["SAM SR"] = true, ["SAM TR"] = true,}
    if tgtGP and fGP then
        local targets = {}
        local units = tgtGP:getUnits()
        local anyFound = false
        for i = 1, #units do
            for atName, _ in pairs(attribs) do
                if units[i]:hasAttribute(atName) then
                    targets[units[i]:getName()] = units[i]:getID()
                    anyFound = true
                end
            end
        end
        if anyFound == true then
            local fCon = fGP:getController()
            for tName, tId in pairs(targets) do
             fCon:pushTask({ 
                  id = 'AttackUnit', 
                  params = { 
                    unitId = tId, 
                    weaponType = 4161536, -- any ASM
                    expend = "Two",
                    attackQtyLimit = true, 
                    attackQty = 1, 
                 } 
                }
             )
            end
        end
    end
end
Notes: Need to test, but I believe there is a possibility that you can assign friendly units as a valid target and the AI will engage said group with this task. Reason 138 why LUA can be more fun than the mission editor.

See article Mission Editor: AI Tasking for more information on how AI tasking is handled within DCS.

Task List: Task Wrappers: mission, ComboTask, ControlledTask, WrappedAction


Tasks: AttackGroup, AttackUnit, Bombing, Strafing, CarpetBombing, AttackMapObject, BombingRunway, orbit, refueling, land, follow, followBigFormation, escort, Embarking, fireAtPoint, hold, FAC_AttackGroup, EmbarkToTransport, DisembarkFromTransport, CargoTransportation, goToWaypoint, groundEscort, RecoveryTanker

En-route Tasks: engageTargets, engageTargetsInZone, engageGroup, engageUnit, awacs, tanker, ewr, FAC_engageGroup, FAC

Commands: script, setCallsign, setFrequency, setFrequencyForUnit, switchWaypoint, stopRoute, switchAction, setInvisible, setImmortal, setUnlimitedFuel, activateBeacon, deactivateBeacon, activateICLS, deactivateICLS, eplrs, start, transmitMessage, stopTransmission, smoke_on_off, ActivateLink4, deactivateLink4, activateACLS, deactivateACLS, LoadingShip

Options: ROE, Reaction To Threat, Radar Using, Flare Using, Formation, RTB On Bingo, silence, Disperse on Attack, Alarm State, RTB on Out of Ammo, ECM Using, Prohibit AA, Prohibit Jettison, Prohibit Afterburner, Prohibit AG, Missile Attack Range, Prohibit WP Pass Report, Engage Air Weapons, Option Radio Usage Contact, Option Radio Usage Engage, Option Radio Usage Kill, AC Engagement Range Restriction, jett tanks if empty, forced attack, Altitude Restriction for AAA Min, restrict targets, Altitude Restriction for AAA Max, Prefer Vertical, Evasion Of ARM, Formation Interval