Skip to content

Commit

Permalink
Units cannot guard themselves.
Browse files Browse the repository at this point in the history
Small line moves that start and end on the same unit now give move commands rather than a guard command (by default).
Right clicking a unit with that only that unit selected issues a move command, rather than doing nothing.
  • Loading branch information
GoogleFrog committed Jun 11, 2021
1 parent efd2007 commit 43e980b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 39 deletions.
36 changes: 21 additions & 15 deletions LuaRules/Gadgets/cmd_areaguard.lua
Expand Up @@ -123,11 +123,13 @@ end

local function DoAreaGuard(unitID, unitDefID, unitTeam, cmdParams, cmdOptions )
local cmdOptions2 = cmdOptions.coded

if #cmdParams == 1 then
spGiveOrderToUnit(unitID, CMD_GUARD, cmdParams[1], cmdOptions2)
return
end

if #cmdParams == 1 then
if unitID ~= cmdParams[1] then
spGiveOrderToUnit(unitID, CMD_GUARD, cmdParams[1], cmdOptions2)
end
return
end

if (not cmdOptions.shift) then
spGiveOrderToUnit(unitID, CMD_STOP, 0, cmdOptions2)
Expand Down Expand Up @@ -340,12 +342,12 @@ end

function gadget:UnitCreated(unitID, unitDefID, team)
local cmdDescID = Spring.FindUnitCmdDesc(unitID, CMD.GUARD)
if cmdDescID then
local cmdArray = {hidden = true}
Spring.EditUnitCmdDesc(unitID, cmdDescID, cmdArray)
if cmdDescID then
local cmdArray = {hidden = true}
Spring.EditUnitCmdDesc(unitID, cmdDescID, cmdArray)
Spring.InsertUnitCmdDesc(unitID, 500, areaGuardCmd)
Spring.InsertUnitCmdDesc(unitID, 501, orbitDrawCmd)
end
end
end

function gadget:UnitDestroyed(unitID, unitDefID, team)
Expand All @@ -355,18 +357,22 @@ function gadget:UnitDestroyed(unitID, unitDefID, team)
end

function gadget:AllowCommand_GetWantedCommand()
return {[CMD_AREA_GUARD] = true, [CMD_ORBIT] = true, [CMD_ORBIT_DRAW] = true}
return {[CMD_GUARD] = true, [CMD_AREA_GUARD] = true, [CMD_ORBIT] = true, [CMD_ORBIT_DRAW] = true}
end

function gadget:AllowCommand_GetWantedUnitDefID()
return true
end

function gadget:AllowCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOptions)
if cmdID == CMD_AREA_GUARD then
DoAreaGuard(unitID, unitDefID, unitTeam, cmdParams, cmdOptions )
return false
elseif cmdID == CMD_ORBIT or cmdID == CMD_ORBIT_DRAW then
if cmdID == CMD_GUARD then
if #cmdParams == 1 and unitID == cmdParams[1] then
return false
end
elseif cmdID == CMD_AREA_GUARD then
DoAreaGuard(unitID, unitDefID, unitTeam, cmdParams, cmdOptions )
return false
elseif cmdID == CMD_ORBIT or cmdID == CMD_ORBIT_DRAW then
return canGuardUnitDefIDs[unitDefID] and unitID ~= cmdParams[1]
end
return true
Expand Down
71 changes: 47 additions & 24 deletions LuaUI/Widgets/cmd_customformations2.lua
Expand Up @@ -2,8 +2,8 @@ function widget:GetInfo()
return {
name = "CustomFormations2",
desc = "Allows you to draw a formation line:"..
"\n mouse drag draw various command on ground."..
"\n ALT+Attack draw attack command on the ground.",
"\n mouse drag draw various command on ground."..
"\n ALT+Attack draw attack command on the ground.",
author = "Niobium, modified by Skasi", -- Based on 'Custom Formations' by jK and gunblob
version = "v3.4", -- With modified dot drawing from v4.3
date = "Mar, 2010",
Expand All @@ -20,8 +20,12 @@ VFS.Include("LuaRules/Configs/customcmds.h.lua")
-- Epic Menu Options
--------------------------------------------------------------------------------

local overrideCmdSingleUnit = {
[CMD.GUARD] = true,
}

options_path = 'Settings/Interface/Command Visibility'--/Formations'
options_order = { 'drawmode_v2', 'linewidth', 'dotsize' }
options_order = { 'drawmode_v2', 'linewidth', 'dotsize', 'overrideGuard' }
options = {
drawmode_v2 = {
name = 'Draw mode',
Expand Down Expand Up @@ -52,6 +56,22 @@ options = {
value = 1,
min = 0.5, max = 2, step=0.1,
},
overrideGuard = {
name = "Override Guard on single unit",
desc = "When enabled, dragging a short line on a unit will give move commands rather than a guard command.",
type = "bool",
value = true,
path = 'Settings/Interface/Commands',
OnChange = function (self)
if self.value then
overrideCmdSingleUnit = {
[CMD.GUARD] = true,
}
else
overrideCmdSingleUnit = {}
end
end,
},
}

--------------------------------------------------------------------------------
Expand All @@ -67,9 +87,11 @@ local minFormationLength = 20
local maxHngTime = 0.05 -- Desired maximum time for hungarian algorithm
local maxNoXTime = 0.05 -- Strict maximum time for backup algorithm

local defaultHungarianUnits = 20 -- Need a baseline to start from when no config data saved
local minHungarianUnits = 10 -- If we kept reducing maxUnits it can get to a point where it can never increase, so we enforce minimums on the algorithms.
local unitIncreaseThresh = 0.85 -- We only increase maxUnits if the units are great enough for time to be meaningful
local defaultHungarianUnits = 20 -- Need a baseline to start from when no config data saved
local minHungarianUnits = 10 -- If we kept reducing maxUnits it can get to a point where it can never increase, so we enforce minimums on the algorithms.
local unitIncreaseThresh = 0.85 -- We only increase maxUnits if the units are great enough for time to be meaningful

local SMALL_FORMATION_THRESHOLD = 8 -- For guard override.

-- Alpha loss per second after releasing mouse
local lineFadeRate = 2.0
Expand Down Expand Up @@ -107,10 +129,10 @@ local overrideCmds = {
}

-- What commands are issued at a position or unit/feature ID (Only used by GetUnitPosition)
local positionCmds = {
[CMD.MOVE]=true, [CMD_RAW_MOVE]=true, [CMD_RAW_BUILD]=true, [CMD.ATTACK]=true, [CMD.RECLAIM]=true, [CMD.RESTORE]=true, [CMD.RESURRECT]=true,
[CMD.PATROL]=true, [CMD.CAPTURE]=true, [CMD.FIGHT]=true, [CMD.MANUALFIRE]=true, [CMD_JUMP]=true, -- jump
[CMD.UNLOAD_UNIT]=true, [CMD.UNLOAD_UNITS]=true,[CMD.LOAD_UNITS]=true, [CMD.GUARD]=true, [CMD.AREA_ATTACK] = true,
local positionCmds = {
[CMD.MOVE] = true, [CMD_RAW_MOVE] = true, [CMD_RAW_BUILD] = true, [CMD.ATTACK] = true, [CMD.RECLAIM] = true, [CMD.RESTORE] = true,
[CMD.PATROL] = true, [CMD.CAPTURE] = true, [CMD.FIGHT] = true, [CMD.MANUALFIRE] = true, [CMD_JUMP] = true, [CMD.RESURRECT] = true,
[CMD.UNLOAD_UNIT] = true, [CMD.UNLOAD_UNITS] = true,[CMD.LOAD_UNITS] = true, [CMD.GUARD] = true, [CMD.AREA_ATTACK] = true,
}

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -503,11 +525,12 @@ function widget:MousePress(mx, my, mButton)
end

local _, defaultCmdID = spGetDefaultCommand()
if not defaultCmdID then return false end
if not defaultCmdID then
return false
end

local overrideCmdID = overrideCmds[defaultCmdID]
if overrideCmdID then

local targType, targID = CulledTraceScreenRay(mx, my, false, inMinimap)
if targType == 'unit' then
overriddenCmd = defaultCmdID
Expand Down Expand Up @@ -659,8 +682,7 @@ function widget:MouseRelease(mx, my, mButton)
local usingFormation = true

-- Override checking
if overriddenCmd then

if overriddenCmd and ((not overrideCmdSingleUnit[overriddenCmd]) or #fNodes < SMALL_FORMATION_THRESHOLD) then
local targetID
local targType, targID = CulledTraceScreenRay(mx, my, false, inMinimap)
if targType == 'unit' then
Expand All @@ -670,23 +692,24 @@ function widget:MouseRelease(mx, my, mButton)
end

if targetID and targetID == overriddenTarget then

-- Signal that we are no longer using the drawn formation
usingFormation = false

-- Process the original command instead
local cmdOpts = GetCmdOpts(alt, ctrl, meta, shift, usingRMB)
GiveNotifyingOrder(overriddenCmd, {overriddenTarget}, cmdOpts)
local selectedUnits = Spring.GetSelectedUnits()
-- The overridden commands cannot be self-issued, so give a move command instead.
if not (#selectedUnits == 1 and selectedUnits[1] == targetID) then
-- Signal that we are no longer using the drawn formation
usingFormation = false

-- Process the original command instead
local cmdOpts = GetCmdOpts(alt, ctrl, meta, shift, usingRMB)
GiveNotifyingOrder(overriddenCmd, {overriddenTarget}, cmdOpts)
end
end
end

-- Using path? If so then we do nothing
if draggingPath then

draggingPath = false

-- Using formation? If so then it's time to calculate and issue orders.
elseif usingFormation then
-- Using formation? If so then it's time to calculate and issue orders.

-- Add final position (Sometimes we don't get the last MouseMove before this MouseRelease)
if (not inMinimap) or spIsAboveMiniMap(mx, my) then
Expand Down

0 comments on commit 43e980b

Please sign in to comment.