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

Close automated attack move loophole #6653

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/snippets/fix.6653.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- (#6653) Close a loophole that allowed UI lua to issue attack move orders.
6 changes: 3 additions & 3 deletions engine/Sim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -727,11 +727,11 @@ end
function IsUnit(object)
end

--- Orders a group of units to attack-move to a position
--- Orders a group of units to attack-move to a target
---@param units Unit[]
---@param position Vector
---@param target Unit | Vector | Prop | Blip
---@return SimCommand
function IssueAggressiveMove(units, position)
function IssueAggressiveMove(units, target)
end

--- Orders a group of units to attack a target
Expand Down
20 changes: 18 additions & 2 deletions lua/SimCallbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local SimPing = import("/lua/simping.lua")
local SimTriggers = import("/lua/scenariotriggers.lua")
local SUtils = import("/lua/ai/sorianutilities.lua")
local ScenarioFramework = import("/lua/scenarioframework.lua")
local UnitQueueDataToCommand = import("/lua/sim/commands/shared.lua").UnitQueueDataToCommand

-- upvalue table operations for performance
local TableInsert = table.insert
Expand All @@ -26,6 +27,7 @@ local TableMerged = table.merged

-- upvalue scope for performance
local type = type
local TableGetn = table.getn
local Vector = Vector
local IsEntity = IsEntity
local GetEntityById = GetEntityById
Expand Down Expand Up @@ -264,14 +266,28 @@ Callbacks.ValidateAssist = function(data, units)
end
end

--- Attack move doesn't exist as a command mode, so this callback substitutes that.
---@param data { Clear: boolean }
---@param units Unit[]
Callbacks.AttackMove = function(data, units)
-- exclude structures as it makes no sense to apply a move command to them
-- exclude structures as they can't move and there is no sim command to issue attack move rally points for factories
local allNonStructures = EntityCategoryFilterDown(categories.ALLUNITS - categories.STRUCTURE, units)

-- Verify that the user manually clicked to issue the command, and use that position.
-- assume all units in the selection were given the same order, so we only need to check one unit
local commandQueue = allNonStructures[1]:GetCommandQueue()
local lastcommand = commandQueue[TableGetn(commandQueue)]
LOG(repr(lastcommand), debug.traceback())
-- dummy script task should be used, although we can't check the script task's type
if UnitQueueDataToCommand[lastcommand.commandType].Type ~= "Script" then return end
-- script tasks issued without a target have x,y,z = 0
local x, y, z = lastcommand.x, lastcommand.y, lastcommand.z
if x == 0 and y == 0 and z == 0 then return end

if data.Clear then
IssueClearCommands(allNonStructures)
end
IssueAggressiveMove(allNonStructures, data.Target)
IssueAggressiveMove(allNonStructures, { x, y, z })
end

--tells a unit to toggle its pointer
Expand Down
49 changes: 45 additions & 4 deletions lua/sim/commands/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,53 @@
--** SOFTWARE.
--******************************************************************************************************

---@alias DistributeOrderInfoCommandName
---| "Stop" # 1
---| "Move" # 2
---| "Dive" # 3
---| "FormMove" # 4
---| "BuildSiloTactical" # 5
---| "BuildSiloNuke" # 6
---| "BuildFactory" # 7
---| "BuildMobile" # 8
---| "BuildAssist" # 9
---| "Attack" # 10
---| "FormAttack" # 11
---| "Nuke" # 12
---| "Tactical" # 13
---| "Teleport" # 14
---| "Guard" # 15
---| "Patrol" # 16
---| "Ferry" # 17
---| "FormPatrol" # 18
---| "Reclaim" # 19
---| "Repair" # 20
---| "Capture" # 21
---| "TransportLoadUnits" # 22
---| "TransportReverseLoadUnits" # 23
---| "TransportUnloadUnits" # 24
---| "TransportUnloadSpecificUnits" # 25
---| "DetachFromTransport" # 26
---| "Upgrade" # 27
---| "Script" # 28
---| "AssistCommander" # 29
---| "KillSelf" # 30
---| "DestroySelf" # 31
---| "Sacrifice" # 32
---| "Pause" # 33
---| "OverCharge" # 34
---| "AggressiveMove" # 35
---| "FormAggressiveMove" # 36
---| "AssistMove" # 37
---| "SpecialAction" # 38
---| "Dock" # 39

---@class DistributeOrderInfo
---@field Callback? fun(units: Unit[], target: Vector | Entity, arg3?: any, arg4?: any): boolean
---@field Type string # Describes the intended order, useful for debugging
---@field BatchOrders boolean # When set, assigns orders to groups of units
---@field FullRedundancy boolean # When set, attempts to add full redundancy when reasonable by assigning multiple orders to each group
---@field Redundancy number # When set, assigns orders to individual units. Number of orders assigned is equal to the redundancy factor
---@field Type DistributeOrderInfoCommandName # Describes the intended order, useful for debugging
---@field BatchOrders boolean # When set, assigns orders to groups of units
---@field FullRedundancy boolean # When set, attempts to add full redundancy when reasonable by assigning multiple orders to each group
---@field Redundancy number # When set, assigns orders to individual units. Number of orders assigned is equal to the redundancy factor

-- upvalue scope for performance
local IssueNuke = IssueNuke
Expand Down
4 changes: 4 additions & 0 deletions lua/ui/game/commandgraph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
--* Copyright © 2006 Gas Powered Games, Inc. All rights reserved.
--*****************************************************************************

-- About dragging command graph nodes:
-- If the target of an order is a unit/blip, its node will snap to units/blips in the same army.
-- If the target of an order is a prop, its node cannot be dragged.

local UIUtil = import("/lua/ui/uiutil.lua")
local Group = import("/lua/maui/group.lua").Group
local Bitmap = import("/lua/maui/bitmap.lua").Bitmap
Expand Down
20 changes: 2 additions & 18 deletions lua/ui/game/commandmode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -603,24 +603,8 @@ end
local function OnScriptIssued(command)
if command.LuaParams then
if command.LuaParams.TaskName == 'AttackMove' then
local avgPoint = { 0, 0 }
for _, unit in command.Units do
avgPoint[1] = avgPoint[1] + unit:GetPosition()[1]
avgPoint[2] = avgPoint[2] + unit:GetPosition()[3]
end
avgPoint[1] = avgPoint[1] / TableGetN(command.Units)
avgPoint[2] = avgPoint[2] / TableGetN(command.Units)

avgPoint[1] = command.Target.Position[1] - avgPoint[1]
avgPoint[2] = command.Target.Position[3] - avgPoint[2]

local rotation = MathAtan(avgPoint[1] / avgPoint[2])
rotation = rotation * 180 / MathPi
if avgPoint[2] < 0 then
rotation = rotation + 180
end
local cb = { Func = "AttackMove", Args = { Target = command.Target.Position, Rotation = rotation,
Clear = command.Clear } }
---@type SimCallback
local cb = { Func = "AttackMove", Args = { Clear = command.Clear } }
SimCallback(cb, true)
elseif command.LuaParams.Enhancement then
EnhancementQueueFile.enqueueEnhancement(command.Units, command.LuaParams.Enhancement)
Expand Down
Loading