Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split cmd_disable_attack from unit_thrower. #4646

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
48 changes: 1 addition & 47 deletions LuaRules/Gadgets/unit_thrower.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,9 @@ if (gadgetHandler:IsSyncedCode()) then
-------------------------------------------------------------------------------------

include("LuaRules/Configs/customcmds.h.lua")
local spFindUnitCmdDesc = Spring.FindUnitCmdDesc
local spEditUnitCmdDesc = Spring.EditUnitCmdDesc
local spInsertUnitCmdDesc = Spring.InsertUnitCmdDesc

local CMD_ATTACK = CMD.ATTACK
local CMD_INSERT = CMD.INSERT

local unitBlockAttackCmd = {
id = CMD_DISABLE_ATTACK,
type = CMDTYPE.ICON_MODE,
name = 'Disable Attack',
action = 'disableattack',
tooltip = 'Allow attack commands',
params = {0, 'Allowed','Blocked'}
}

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

Expand Down Expand Up @@ -258,23 +245,8 @@ end
--------------------------------------------------------------------------------
-- Command Handling

local function BlockAttackToggle(unitID, cmdParams)
local data = IterableMap.Get(throwUnits, unitID)
if data then
local state = cmdParams[1]
local cmdDescID = spFindUnitCmdDesc(unitID, CMD_DISABLE_ATTACK)

if (cmdDescID) then
unitBlockAttackCmd.params[1] = state
spEditUnitCmdDesc(unitID, cmdDescID, { params = unitBlockAttackCmd.params})
end
data.blockAttack = (state == 1)
end
end

function gadget:AllowCommand_GetWantedCommand()
return {
[CMD_DISABLE_ATTACK] = true,
[CMD_ATTACK] = true,
[CMD_INSERT] = true,
[CMD_UNIT_SET_TARGET] = true,
Expand Down Expand Up @@ -303,11 +275,7 @@ function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOpt
return true -- command was not used
end

if (cmdID ~= CMD_DISABLE_ATTACK) then
return true -- command was not used
end
BlockAttackToggle(unitID, cmdParams)
return false -- command was used
return true -- command was not used
end

----------------------------------------------------------------------------------------------
Expand All @@ -323,9 +291,6 @@ function gadget:UnitCreated(unitID, unitDefID, teamID)
weaponNum = 1,
}
)

spInsertUnitCmdDesc(unitID, unitBlockAttackCmd)
BlockAttackToggle(unitID, {0})
end
end

Expand All @@ -338,25 +303,14 @@ function gadget:UnitDestroyed(unitID, unitDefID)
end
end

local externalFunc = {}
function externalFunc.BlockAttack(unitID)
local unitData = unitID and IterableMap.Get(throwUnits, unitID)
return unitData and unitData.blockAttack
end

function gadget:Initialize()
-- register command
gadgetHandler:RegisterCMDID(CMD_DISABLE_ATTACK)

for _, unitID in pairs(Spring.GetAllUnits()) do
gadget:UnitCreated(unitID, Spring.GetUnitDefID(unitID), Spring.GetUnitTeam(unitID))
end

for id, _ in pairs(throwWeaponDef) do
Script.SetWatchProjectile(id, true)
end

GG.Thrower = externalFunc
end

local function UpdateTrajectory(unitID, data)
Expand Down
18 changes: 6 additions & 12 deletions LuaUI/Widgets/cmd_area_attack_tweak.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ function widget:CommandNotify(id, params, options) --ref: gui_tacticalCalculator
if not defaultCommands[id] or options.internal then --only process user's command
return false
end
if Spring.GetSelectedUnitsCount() == 0 then --skip whole thing if no selection
local units = WG.units or Spring.GetSelectedUnits()
if #units == 0 then --skip whole thing if no selection
return false
end
local units
if SPLIT_ATTACK_SINGLE and handledCount > 0 then
--This remove all but 1st attack order from CTRL+Area_attack if user choose to append new order to unit (eg: SHIFT+move),
--this is to be consistent with bomber_command (rearm-able bombers), which only shoot 1st target and move on to next order.

--Known limitation: not able to remove order if user queued faster than network delay (it need to see unit's current command queue)
units = Spring.GetSelectedUnits()
local unitID, attackList
for i=1,#units do
unitID = units[i]
Expand All @@ -73,7 +72,6 @@ function widget:CommandNotify(id, params, options) --ref: gui_tacticalCalculator
--The following code filter out ground unit from dedicated AA, and
--split target among selected unit if user press CTRL+Area_attack
local cx2, cy2, cz2 = params[4], params[5], params[6]
units = units or Spring.GetSelectedUnits()
local targetUnits
if cz2 then
targetUnits = Spring.GetUnitsInRectangle(math.min(cx,cx2), math.min(cz,cz2), math.max(cx,cx2), math.max(cz,cz2))
Expand Down Expand Up @@ -193,14 +191,10 @@ function ReIssueCommandsToUnits(antiAirUnits,airTargets,normalUnits,allTargets,c
IssueSplitedCommand(normalUnits,allTargets,cmdID,options)
isHandled = true
else -- normal queue
if #antiAirUnits>1 then
--split between AA and ground,
IssueCommand(antiAirUnits,airTargets,cmdID,options)
IssueCommand(normalUnits,allTargets,cmdID,options)
isHandled = true
else
isHandled = false --nothing need to be done, let spring handle
end
--split between AA and ground,
IssueCommand(antiAirUnits,airTargets,cmdID,options)
IssueCommand(normalUnits,allTargets,cmdID,options)
isHandled = true
end
return isHandled
end
Expand Down
2 changes: 1 addition & 1 deletion LuaUI/Widgets/cmd_customformations2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ end

local function GetExecutingUnits(cmdID)
local units = {}
local selUnits = spGetSelectedUnits()
local selUnits = (WG.CmdDisableAttack and WG.CmdDisableAttack.FilterSelectedUnits(cmdID)) or spGetSelectedUnits()
for i = 1, #selUnits do
local uID = selUnits[i]
if CanUnitExecute(uID, cmdID) then
Expand Down
133 changes: 133 additions & 0 deletions LuaUI/Widgets/cmd_disable_attack.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

function widget:GetInfo()
return {
name = "Disable attack command",
desc = "Implements disable attack command",
author = "Google Frog",
date = "12 Janurary 2018",
license = "GNU GPL, v2 or later",
layer = -100,
handler = true,
enabled = true -- loaded by default?
}
end

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

local attackDisabableUnitTypes = {}

for i = 1, #UnitDefs do
local ud = UnitDefs[i]
if ud.customParams.can_disable_attack then
attackDisabableUnitTypes[i] = true
end
end

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

VFS.Include("LuaRules/Configs/customcmds.h.lua")
local CMD_ATTACK = CMD.ATTACK
local CMD_INSERT = CMD.INSERT
local CMD_MANUALFIRE = CMD.MANUALFIRE

-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

local attackDisabledUnits = {}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Command Handling
function SetDisableAttack(unit, value)
if attackDisabledUnits[unit] then
attackDisabledUnits[unit] = value
end
end

function widget:CommandsChanged()
local units = Spring.GetSelectedUnits()
for i = 1, #units do
if attackDisabledUnits[units[i]] then
local customCommands = widgetHandler.customCommands
customCommands[#customCommands+1] = {
id = CMD_DISABLE_ATTACK,
type = CMDTYPE.ICON_MODE,
name = 'Disable Attack',
action = 'disableattack',
tooltip = 'Allow attack commands',
params = {attackDisabledUnits[units[i]], 'Allowed', 'Blocked'}
}
return
end
end
end

function FilterSelectedUnits(cmdID)
if cmdID ~= CMD_ATTACK and cmdID ~= CMD_UNIT_SET_TARGET and cmdID ~= CMD_UNIT_SET_TARGET_CIRCLE then
return Spring.GetSelectedUnits()
end
local units = Spring.GetSelectedUnits()
local selected = {}
for i = 1, #units do
if attackDisabledUnits[units[i]] ~= 1 then
selected[#selected + 1] = units[i]
end
end

return selected
end

function widget:CommandNotify(cmdID, cmdParams, cmdOptions)
if cmdID ~= CMD_DISABLE_ATTACK then
-- TODO: handle commands imbeded in CMD_INSERT.
WG.units = FilterSelectedUnits(cmdID)
return
end

local units = WG.units or Spring.GetSelectedUnits()

for i = 1, #units do
SetDisableAttack(units[i], cmdParams[1])
end

return true
end

function widget:UnitCommandNotify(unitID, id, params, options)
if id ~= CMD_MANUALFIRE then
return false
end

if attackDisabledUnits[unitID] then
Spring.GiveOrderToUnit(unitID, CMD_ATTACK, params, options)
return true
end
return false
end

----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
-- Unit Handler

function widget:UnitCreated(unitID, unitDefID, teamID)
if attackDisabableUnitTypes[unitDefID] then
attackDisabledUnits[unitID] = 0
end
end

function widget:UnitDestroyed(unitID, unitDefID)
attackDisabledUnits[unitID] = nil
end

function widget:Initialize()
for _, unitID in pairs(Spring.GetAllUnits()) do
widget:UnitCreated(unitID, Spring.GetUnitDefID(unitID), Spring.GetUnitTeam(unitID))
end

WG.CmdDisableAttack = {
FilterSelectedUnits = FilterSelectedUnits,
SetDisableAttack = SetDisableAttack
}
end
6 changes: 3 additions & 3 deletions LuaUI/Widgets/cmd_keep_target.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ local orderParamTable = {0}
local CMD_OPT_INTERNAL = CMD.OPT_INTERNAL
function widget:CommandNotify(cmdID, cmdParams, cmdOptions)
if TargetKeepingCommand[cmdID] and options.keepTarget.value then
local units = Spring.GetSelectedUnits()
local units = WG.units or Spring.GetSelectedUnits()
for i = 1, #units do
local unitID = units[i]
if isValidUnit(unitID) then
Expand All @@ -119,7 +119,7 @@ function widget:CommandNotify(cmdID, cmdParams, cmdOptions)
end
end
elseif TargetIssuingCommand[cmdID] and options.keepTarget.value and (not cmdOptions.shift) and cmdParams and #cmdParams == 1 then
local units = Spring.GetSelectedUnits()
local units = WG.units or Spring.GetSelectedUnits()
orderParamTable[1] = cmdParams[1]
for i = 1, #units do
local unitID = units[i]
Expand All @@ -129,7 +129,7 @@ function widget:CommandNotify(cmdID, cmdParams, cmdOptions)
end
--Spring.GiveOrderToUnitArray(units, CMD_UNIT_SET_TARGET, orderParamTable, CMD_OPT_INTERNAL)
elseif TargetCancelingCommand[cmdID] and options.removeTarget.value and not (cmdOptions and cmdOptions.shift) then
local units = Spring.GetSelectedUnits()
local units = WG.units or Spring.GetSelectedUnits()
Spring.GiveOrderToUnitArray(units, CMD_UNIT_CANCEL_TARGET, cmdParams, 0)
end
return false
Expand Down
30 changes: 30 additions & 0 deletions LuaUI/Widgets/cmd_unhandled.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function widget:GetInfo()
return {
name = "Unhandled Commands",
desc = "handles unhandled commands.",
author = "Amnykon",
date = "March 22, 2022",
license = "GNU GPL, v2 or later",
layer = math.huge,
handler = true,
enabled = true -- loaded by default?
}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:CommandNotify(id, params, options)
local units = WG.units or Spring.GetSelectedUnits()
WG.units = nil

for i=1,#units do
widgetHandler:UnitCommandNotify(units[i], id, params, options)
end
return true
end

function widget:UnitCommandNotify(unitID, id, params, options)
Spring.GiveOrderToUnit (unitID, id, params, options)
return true
end

6 changes: 3 additions & 3 deletions LuaUI/Widgets/unit_start_state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ local function addUnit(defName, path)
end
end

if ud.customParams.attack_toggle then
if ud.customParams.can_disable_attack then
options[defName .. "_disableattack"] = {
name = " Disable Attack Commands",
desc = "Check the box to make the unit not respond to attack commands.",
Expand Down Expand Up @@ -1417,8 +1417,8 @@ function widget:UnitCreated(unitID, unitDefID, unitTeam, builderID)
end

value = GetStateValue(name, "disableattack")
if value then -- false is the default
orderArray[#orderArray + 1] = {CMD_DISABLE_ATTACK, {1}, CMD.OPT_SHIFT}
if value and WG.CmdDisableAttack then -- false is the default
WG.CmdDisableAttack.SetDisableAttack(unitID, 1)
end

value = GetStateValue(name, "formation_rank")
Expand Down
7 changes: 0 additions & 7 deletions scripts/amphlaunch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ function script.QueryWeapon(num)
end

function script.BlockShot(num, targetID)
if num == 1 and GG.Thrower and GG.Thrower.BlockAttack(unitID) then
return true
end
local x, y, z = spGetUnitPosition(unitID)
if not x then
return true
Expand All @@ -129,10 +126,6 @@ function script.BlockShot(num, targetID)
return true
end
end
local reloadTime = Spring.GetUnitWeaponState(unitID, 1, "reloadTime")*30 -- Takes slow into account
local otherNum = 3 - num
local gameFrame = Spring.GetGameFrame()
Spring.SetUnitWeaponState(unitID, otherNum, "reloadFrame", gameFrame + reloadTime)
return false
end

Expand Down
Loading