Skip to content

Commit

Permalink
Add ringing of radar and artillery (FAForever#5391)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garanas authored Sep 4, 2023
1 parent dba25b9 commit e5a4c05
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 18 deletions.
56 changes: 56 additions & 0 deletions lua/SimCallbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,62 @@ Callbacks.RingWithFabricators = function(data, selection)
import("/lua/sim/commands/ringing/ring-with-fabricators.lua").RingExtractor(extractor, engineers, data.allFabricators)
end

---@param data { target: EntityId }
---@param selection Unit[]
Callbacks.RingRadar = function(data, selection)
-- verify selection
selection = SecureUnits(selection)
if (not selection) or TableEmpty(selection) then
return
end

-- verify we have engineers
local engineers = EntityCategoryFilterDown(categories.ENGINEER, selection)
if TableEmpty(engineers) then
return
end

-- verify the extractor
local target = GetUnitById(data.target) --[[@as Unit]]
if (not target) or
(not target.Army) or
(not OkayToMessWithArmy(target.Army)) or
(not EntityCategoryContains((categories.RADAR + categories.OMNI) * categories.STRUCTURE, target))
then
return
end

import("/lua/sim/commands/ringing/ring-with-power.lua").RingWithPower(target, engineers)
end

---@param data { target: EntityId }
---@param selection Unit[]
Callbacks.RingArtilleryTech2 = function(data, selection)
-- verify selection
selection = SecureUnits(selection)
if (not selection) or TableEmpty(selection) then
return
end

-- verify we have engineers
local engineers = EntityCategoryFilterDown(categories.ENGINEER, selection)
if TableEmpty(engineers) then
return
end

-- verify the extractor
local target = GetUnitById(data.target) --[[@as Unit]]
if (not target) or
(not target.Army) or
(not OkayToMessWithArmy(target.Army)) or
(not EntityCategoryContains(categories.ARTILLERY * categories.TECH2 * categories.STRUCTURE, target))
then
return
end

import("/lua/sim/commands/ringing/ring-with-power.lua").RingWithPower(target, engineers)
end

---@param data any
---@param selection Unit[]
Callbacks.SelectHighestEngineerAndAssist = function(data, selection)
Expand Down
45 changes: 44 additions & 1 deletion lua/options/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,17 @@ options = {
},

{
title = "<LOC OPTIONS_0273>Right click to ring extractors with storages",
title = "<LOC structure_ringing_extractor_title>Right click to ring extractors with storages",
key = 'structure_capping_feature_01',
type = 'toggle',
default = "on",
set = function(key, value, startup)
if GetCurrentUIState() == 'game' then
import("/lua/ui/game/hotkeys/ring-extractor.lua").RingStorages = value == 'on' or value == 'on-inner' or value == 'on-all'
import("/lua/ui/game/hotkeys/ring-extractor.lua").RingFabricatorsInner = value == 'on-inner'
import("/lua/ui/game/hotkeys/ring-extractor.lua").RingFabricatorsAll = value == 'on-all'
end
end,
custom = {
states = {
{ text = "<LOC _Off>Off", key = "off" },
Expand All @@ -539,6 +546,42 @@ options = {
},
},

{
title = "<LOC structure_ringing_radar_title>Right click to ring radar with power",
key = 'structure_ringing_radar',
type = 'toggle',
default = "on",
set = function(key, value, startup)
if GetCurrentUIState() == 'game' then
import("/lua/ui/game/hotkeys/ring-extractor.lua").RingRadars = value == 'on'
end
end,
custom = {
states = {
{ text = "<LOC _Off>Off", key = "off" },
{ text = "<LOC _On>On", key = "on" },
},
},
},

{
title = "<LOC structure_ringing_artillery_title>Right click to ring artillery with power",
key = 'structure_ringing_artillery',
type = 'toggle',
default = "on",
set = function(key, value, startup)
if GetCurrentUIState() == 'game' then
import("/lua/ui/game/hotkeys/ring-extractor.lua").RingArtillery = value == 'on'
end
end,
custom = {
states = {
{ text = "<LOC _Off>Off", key = "off" },
{ text = "<LOC _On>On", key = "on" },
},
},
},

{
title = "<LOC ASSIST_TO_UPGRADE>Hold alt to force attack move",
key = 'alt_to_force_attack_move',
Expand Down
51 changes: 51 additions & 0 deletions lua/sim/commands/ringing/ring-with-power.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

--******************************************************************************************************
--** Copyright (c) 2022 Willem 'Jip' Wijnia
--**
--** Permission is hereby granted, free of charge, to any person obtaining a copy
--** of this software and associated documentation files (the "Software"), to deal
--** in the Software without restriction, including without limitation the rights
--** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
--** copies of the Software, and to permit persons to whom the Software is
--** furnished to do so, subject to the following conditions:
--**
--** The above copyright notice and this permission notice shall be included in all
--** copies or substantial portions of the Software.
--**
--** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
--** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
--** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
--** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
--** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
--** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
--** SOFTWARE.
--******************************************************************************************************

local SortUnitsByTech = import("/lua/sim/commands/shared.lua").SortUnitsByTech
local RingUnit = import("/lua/sim/commands/ringing/shared.lua").RingUnit

local BuildOffsets = { { 2, 0 }, { 0, 2 }, { -2, 0 }, { 0, -2 } }

---@param target Unit
---@param engineers Unit[]
RingWithPower = function(target, engineers)

---------------------------------------------------------------------------
-- defensive programming

-- confirm we have an extractor
if (not target) or (IsDestroyed(target)) then
return
end

-- confirm that we have one engineer that can build the unit
SortUnitsByTech(engineers)
local storage = engineers[1].Blueprint.BlueprintId:sub(1, 2) .. 'b1101'
if (not __blueprints[storage]) or
(not engineers[1]:CanBuild(storage))
then
return
end

RingUnit(target, engineers, BuildOffsets, storage)
end
71 changes: 54 additions & 17 deletions lua/ui/game/hotkeys/ring-extractor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@

local Prefs = import("/lua/user/prefs.lua")

local ringExtractorPrefs = Prefs.GetFromCurrentProfile('options.structure_capping_feature_01')
RingStorages = ringExtractorPrefs == 'on' or ringExtractorPrefs == 'on-inner' or ringExtractorPrefs == 'on-all'
RingFabricatorsInner = ringExtractorPrefs == 'on-inner'
RingFabricatorsAll = ringExtractorPrefs == 'on-all'
RingRadars = Prefs.GetFromCurrentProfile('options.structure_ringing_radar') == 'on'
RingArtillery = Prefs.GetFromCurrentProfile('options.structure_ringing_artillery') == 'on'

--- Allows us to detect a double click
local pStructure1 = nil
local pStructure2 = nil

---@param command UserCommand
function RingExtractor(command)

-- retrieve the option in question, can have values: 'off', 'only-storages-extractors' and 'full-suite'
local option = Prefs.GetFromCurrentProfile('options.structure_capping_feature_01')

-- bail out - we're not interested
if option == 'off' then
return
end

-- check if we have engineers
local units = EntityCategoryFilterDown(categories.ENGINEER, command.Units)
if not units[1] then return end
Expand All @@ -62,11 +61,11 @@ function RingExtractor(command)
if structure:IsInCategory('STRUCTURE') then

-- try and create storages and / or fabricators around it
if structure:IsInCategory('MASSEXTRACTION') then
if (RingStorages or RingFabricatorsInner or RingFabricatorsAll) and structure:IsInCategory('MASSEXTRACTION') then

-- check what type of buildings we'd like to make
local buildFabs =
((option == 'on-inner') or (option == 'on-all'))
(RingFabricatorsInner or RingFabricatorsAll)
and (
(isTech2 and isUpgrading and isTripleTapped and isShiftDown)
or (isTech3 and isDoubleTapped and isShiftDown)
Expand Down Expand Up @@ -115,20 +114,58 @@ function RingExtractor(command)

structure.RingFabsStamp = gameTick

if option == 'on-inner'then
print("Ringing extractor with fabricators")
SimCallback({ Func = 'RingWithFabricators', Args = { target = command.Target.EntityId, allFabricators = false } }, true)
else
print("Ringing extractor with fabricators")
SimCallback({ Func = 'RingWithFabricators', Args = { target = command.Target.EntityId, allFabricators = true } }, true)
end
print("Ringing extractor with fabricators")
SimCallback({ Func = 'RingWithFabricators', Args = { target = command.Target.EntityId, allFabricators = RingFabricatorsAll } }, true)

-- reset state
structure = nil
pStructure1 = nil
pStructure2 = nil
end

elseif RingArtillery and structure:IsInCategory('ARTILLERY') and isTech2 then
-- prevent consecutive calls
local gameTick = GameTick()
if structure.RingStamp then
if structure.RingStamp + 5 > gameTick then
return
end
end

structure.RingStamp = gameTick

print("Ringing with power generators")
SimCallback({ Func = 'RingArtilleryTech2', Args = { target = command.Target.EntityId } }, true)

-- reset state
structure = nil
pStructure1 = nil
pStructure2 = nil
elseif RingRadars and (structure:IsInCategory('RADAR') or structure:IsInCategory('OMNI'))
and ( -- checks for upgrading state
(isTech1 and isUpgrading and isDoubleTapped and isShiftDown)
or (isTech2 and isUpgrading and isDoubleTapped and isShiftDown)
or (isTech2 and not isUpgrading)
or (isTech3)
)
then
-- prevent consecutive calls
local gameTick = GameTick()
if structure.RingStamp then
if structure.RingStamp + 5 > gameTick then
return
end
end

structure.RingStamp = gameTick

print("Ringing with power generators")
SimCallback({ Func = 'RingRadar', Args = { target = command.Target.EntityId } }, true)

-- reset state
structure = nil
pStructure1 = nil
pStructure2 = nil
end
end

Expand Down

0 comments on commit e5a4c05

Please sign in to comment.