diff --git a/LuaRules/Gadgets/unit_thrower.lua b/LuaRules/Gadgets/unit_thrower.lua index 3bb7b165ef..5b6775b4f1 100644 --- a/LuaRules/Gadgets/unit_thrower.lua +++ b/LuaRules/Gadgets/unit_thrower.lua @@ -40,6 +40,8 @@ local IterableMap = VFS.Include("LuaRules/Gadgets/Include/IterableMap.lua") local applyBlockingFrame = {} local unitIsNotBlocking = {} +local cachedAttackCommandDesc = false + ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- -- Constants @@ -64,6 +66,7 @@ local RECENT_INT_WIDTH = 1 local MAX_ALTITUDE_AIM = 60 local NO_BLOCK_TIME = 5 +local ATTACK_BLOCK_DEFAULT = 1 ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- @@ -294,6 +297,16 @@ end -------------------------------------------------------------------------------- -- Command Handling +local function CacheAttackCommandDesc(unitID) + local cmdTable = Spring.GetUnitCmdDescs(unitID) + for i = 1, #cmdTable do + if cmdTable[i].id == CMD.ATTACK then + cachedAttackCommandDesc = cmdTable[i] + return + end + end +end + local function BlockAttackToggle(unitID, cmdParams) local data = IterableMap.Get(throwUnits, unitID) if data then @@ -307,8 +320,17 @@ local function BlockAttackToggle(unitID, cmdParams) if state == 1 then local cmdDesc = spFindUnitCmdDesc(unitID, CMD.ATTACK) if cmdDesc then + if not cachedAttackCommandDesc then + CacheAttackCommandDesc(unitID) + Spring.Utilities.TableEcho(cachedAttackCommandDesc, "cmdDesc") + end spRemoveUnitCmdDesc(unitID, cmdDesc) end + elseif cachedAttackCommandDesc then + local cmdDesc = spFindUnitCmdDesc(unitID, CMD.ATTACK) + if not cmdDesc then + spInsertUnitCmdDesc(unitID, cachedAttackCommandDesc) + end end data.blockAttack = (state == 1) end @@ -367,7 +389,7 @@ function gadget:UnitCreated(unitID, unitDefID, teamID) ) spInsertUnitCmdDesc(unitID, unitBlockAttackCmd) - BlockAttackToggle(unitID, {0}) + BlockAttackToggle(unitID, {ATTACK_BLOCK_DEFAULT}) end end diff --git a/LuaUI/Widgets/unit_start_state.lua b/LuaUI/Widgets/unit_start_state.lua index 0eeebd22d4..93c742f4c0 100644 --- a/LuaUI/Widgets/unit_start_state.lua +++ b/LuaUI/Widgets/unit_start_state.lua @@ -1037,7 +1037,7 @@ local function addUnit(defName, path) end if ud.customParams.attack_toggle then - options[defName .. "_disableattack"] = { + options[defName .. "_disableattack_0"] = { name = " Disable Attack Commands", desc = "Check the box to make the unit not respond to attack commands.", type = 'bool',