Skip to content

Commit

Permalink
moved fighters attackSafetyDistance code from airunitsturnradius gadg…
Browse files Browse the repository at this point in the history
…et into a new dedicated gadget: "Air AttackSafetyDistance"
  • Loading branch information
Ruwetuin committed Jan 21, 2025
1 parent 56de34a commit a9db624
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
49 changes: 49 additions & 0 deletions luarules/gadgets/unit_air_attacksafetydistance.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function gadget:GetInfo()
return {
name = "Air AttackSafetyDistance",
desc = "sets attackSafetyDistance for fighters",
author = "Doo, Floris",
date = "Sept 19th 2017",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true
}
end

--[[
Wiki: "attackSafetyDistance" movetypedata: Fighters abort dive toward target if within attackSafetyDistance and
try to climb back to normal altitude while still moving toward target. It's disabled by default. Set to half of
the minimum weapon range to avoid collisions, enemy fire, AOE damage. If set to greater than the weapon range,
the unit will fly over the target like a bomber.
]]--

if not gadgetHandler:IsSyncedCode() then
return
end

local isFighter = {}
for udid, ud in pairs(UnitDefs) do
if ud.canFly and ud.customParams.fighter then
isFighter[udid] = true
end
end

function gadget:Initialize()
gadgetHandler:RegisterAllowCommand(CMD.ANY)
for ct, unitID in pairs(Spring.GetAllUnits()) do
gadget:UnitCreated(unitID, Spring.GetUnitDefID(unitID))
end
end

function gadget:UnitCreated(unitID, unitDefID)
if isFighter[unitDefID] then
local curMoveCtrl = Spring.MoveCtrl.IsEnabled(unitID)
if curMoveCtrl then
Spring.MoveCtrl.Disable(unitID)
end
Spring.MoveCtrl.SetAirMoveTypeData(unitID, "attackSafetyDistance", 300)
if curMoveCtrl then
Spring.MoveCtrl.Enable(unitID)
end
end
end
16 changes: 1 addition & 15 deletions luarules/gadgets/unit_airunitsturnradius.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function gadget:GetInfo()
return {
name = "TurnRadius",
desc = "Fixes TurnRadius Dynamically for bombers (also sets attackSafetyDistance for fighters)",
desc = "Fixes TurnRadius Dynamically for bombers",
author = "Doo",
date = "Sept 19th 2017",
license = "GNU GPL, v2 or later",
Expand All @@ -25,13 +25,9 @@ local spMoveCtrlDisable = Spring.MoveCtrl.Disable
local spMoveCtrlSetAirMoveTypeData = Spring.MoveCtrl.SetAirMoveTypeData

local Bombers = {}
local isFighter = {}
local bomberTurnRadius = {}
for udid, ud in pairs(UnitDefs) do
if ud.canFly then
if ud.customParams.fighter then
isFighter[udid] = true
end
if not ud.hoverAttack and ud.weapons and ud.weapons[1] then
for i = 1, #ud.weapons do
local wDef = WeaponDefs[ud.weapons[i].weaponDef]
Expand All @@ -55,16 +51,6 @@ function gadget:UnitCreated(unitID, unitDefID)
if bomberTurnRadius[unitDefID] then
Bombers[unitID] = unitDefID
end
if isFighter[unitDefID] then
local curMoveCtrl = spMoveCtrlIsEnabled(unitID)
if curMoveCtrl then
spMoveCtrlDisable(unitID)
end
spMoveCtrlSetAirMoveTypeData(unitID, "attackSafetyDistance", 300) -- Wiki about attackSafetyDistance: Fighters abort dive toward target if within attackSafetyDistance and try to climb back to normal altitude while still moving toward target. It's disabled by default. Set to half of the minimum weapon range to avoid collisions, enemy fire, AOE damage. If set to greater than the weapon range, the unit will fly over the target like a bomber.
if curMoveCtrl then
spMoveCtrlEnable(unitID)
end
end
end

function gadget:UnitDestroyed(unitID)
Expand Down

0 comments on commit a9db624

Please sign in to comment.