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

Fix a bug and extend the context based templates feature #6015

Merged
merged 5 commits into from
Mar 21, 2024
Merged
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
6 changes: 5 additions & 1 deletion changelog/3804.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

- (#6007) Fix Siren uselessly trying to repair units when assisting them

- (#6015) Fix a bug with capping extractors via the context-based templates feature

## Balance

<!-- Remove header when empty -->

## Features

<!-- Remove header when empty -->
- (#6015) Extend the context-based templates feature

When you hover over a unit the first 'template' is always just the 'best fit' that represents that unit. As an example, if you hover over a point defense then it will pick the or a similar point defense that your current selection can build.

## Graphics

Expand Down
86 changes: 66 additions & 20 deletions lua/ui/game/hotkeys/context-based-templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ end

-- convert all known templates

local SingletonTemplate = { TemplateData = { 0, 0, { 'dummy', 0, 0, 0 } }}

---@type table
local RawTemplates = import("/lua/ui/game/hotkeys/context-based-templates-data.lua")

Expand Down Expand Up @@ -98,11 +100,36 @@ CommandMode.AddEndBehavior(
if not table.empty(ContextBasedTemplates) then
ContextBasedTemplates = {}
ContextBasedTemplateStep = 0
ClearBuildTemplates()
end
end,
'ContextBasedTemplates'
)

--- Converts the blueprint id to the preferred faction
---@param blueprintId BlueprintId
---@param prefix 'ua' | 'ue' | 'ur' | 'xs' | 'xn'
---@return BlueprintId
local function ConvertBlueprintId(blueprintId, prefix)
local templateUnitBlueprintId = prefix .. blueprintId:sub(3)

-- because the support factories originate from mods they do not adhere to the
-- standard blueprint convention for factions; very annoying ^^
local isSupportFactory = blueprintId:sub(1, 1) == 'z'
if isSupportFactory then
templateUnitBlueprintId = 'z' .. prefix:sub(2, 2) .. blueprintId:sub(3)
end

-- same here but then for units that are part of the Forged Alliance expansion; they
-- do not adhere to blueprint standards
local isExpansion = blueprintId:sub(1, 1) == 'x' and blueprintId:sub(1, 2) ~= 'xs'
if isExpansion then
templateUnitBlueprintId = 'x' .. prefix:sub(2, 2) .. blueprintId:sub(3)
end

return templateUnitBlueprintId
end

--- Validates the template in-place, returns whether the process succeeded
---@param template ContextBasedTemplate
---@param buildableUnits table<BlueprintId, boolean>
Expand All @@ -114,27 +141,20 @@ local function ValidateTemplate(template, buildableUnits, prefix)

for l = 3, TableGetn(template.TemplateData) do
local templateUnit = template.TemplateData[l]
local templateUnitBlueprintId = prefix .. templateUnit[1]:sub(3)

-- because the support factories originate from mods they do not adhere to the
-- standard blueprint convention for factions; very annoying ^^
local isSupportFactory = templateUnit[1]:sub(1, 1) == 'z'
if isSupportFactory then
templateUnitBlueprintId = 'z' .. prefix:sub(2, 2) .. templateUnit[1]:sub(3)
end

-- same here but then for units that are part of the Forged Alliance expansion; they
-- do not adhere to blueprint standards
local isExpansion = templateUnit[1]:sub(1, 1) == 'x' and templateUnit[1]:sub(1, 2) ~= 'xs'
if isExpansion then
templateUnitBlueprintId = 'x' .. prefix:sub(2, 2) .. templateUnit[1]:sub(3)
end
local templateUnitBlueprintId = ConvertBlueprintId(templateUnit[1], prefix)

-- proceed as usual
-- proceed as usual, try and find a template
---@type UnitBlueprint
local templateUnitBlueprint = __blueprints[templateUnitBlueprintId]
local templateUnitBlueprintFromId = templateUnitBlueprint.General.UpgradesFrom
local templateUnitBlueprintBaseId = templateUnitBlueprint.General.UpgradesFromBase
if templateUnitBlueprint then
if buildableUnits[templateUnitBlueprintId] then
templateUnit[1] = templateUnitBlueprintId
elseif templateUnitBlueprintFromId and buildableUnits[templateUnitBlueprintFromId] then
templateUnit[1] = templateUnitBlueprintFromId
elseif templateUnitBlueprintBaseId and buildableUnits[templateUnitBlueprintBaseId] then
templateUnit[1] = templateUnitBlueprintBaseId
else
allUnitsBuildable = false
end
Expand Down Expand Up @@ -212,23 +232,49 @@ local function FilterTemplatesByUnitContext(buildableUnits, prefix)
-- try and retrieve blueprint id from highlight command
if not blueprintId then
local highlightCommand = GetHighlightCommand()
if highlightCommand.blueprintId then
if highlightCommand and highlightCommand.blueprintId then
blueprintId = highlightCommand.blueprintId
end
end

-- try and retrieve blueprint id from rollover info
if not blueprintId then
local info = GetRolloverInfo()
blueprintId = info.blueprintId
if info.userUnit then
blueprintId = info.blueprintId
end
end

-- if still not available then give up and bail out
if not blueprintId then
return false
end

-- see if any templates match the blueprint id
-- add the blueprint id that we're hovering over
local convertedBlueprintId = ConvertBlueprintId(blueprintId, prefix)
local convertedBlueprint = __blueprints[convertedBlueprintId]
local templateUnitBlueprintFromId = convertedBlueprint.General.UpgradesFrom
local templateUnitBlueprintBaseId = convertedBlueprint.General.UpgradesFromBase

local validBlueprintId
if convertedBlueprint then
if buildableUnits[convertedBlueprintId] then
validBlueprintId = convertedBlueprintId
elseif templateUnitBlueprintFromId and buildableUnits[templateUnitBlueprintFromId] then
validBlueprintId = templateUnitBlueprintFromId
elseif templateUnitBlueprintBaseId and buildableUnits[templateUnitBlueprintBaseId] then
validBlueprintId = templateUnitBlueprintBaseId
end
end

if validBlueprintId then
SingletonTemplate.Name = LOC(__blueprints[validBlueprintId].Description)
SingletonTemplate.TemplateData[3][1] = validBlueprintId
TableInsert(ContextBasedTemplates, SingletonTemplate)
ContextBasedTemplateCount = ContextBasedTemplateCount + 1
end

-- add templates that match the unit that we're hovering over
for k = 1, TableGetn(Templates) do
local template = Templates[k]
local trigger = template.TriggersOnUnit or template.TriggersOnBuilding
Expand Down Expand Up @@ -329,7 +375,7 @@ Cycle = function()
ClearBuildTemplates()
end

print(StringFormat("(%d/%d) %s", index, ContextBasedTemplateCount, template.Name))
print(StringFormat("(%d/%d) %s", index, ContextBasedTemplateCount, tostring(template.Name)))
end

ContextBasedTemplateStep = ContextBasedTemplateStep + 1
Expand Down
1 change: 1 addition & 0 deletions units/XEB0204/XEB0204_unit.bp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ UnitBlueprint{
SelectionPriority = 5,
UnitName = "<LOC xeb0204_name>The Kennel",
UpgradesFrom = "xeb0104",
UpgradesFromBase = "xeb0104",
},
Intel = { VisionRadius = 14 },
LifeBarHeight = 0.075,
Expand Down
1 change: 1 addition & 0 deletions units/XRB0204/XRB0204_unit.bp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ UnitBlueprint{
ToggleCaps = { RULEUTC_ProductionToggle = true },
UnitName = "<LOC xrb0204_name>The Hive",
UpgradesFrom = "xrb0104",
UpgradesFromBase = "xrb0104",
UpgradesTo = "xrb0304",
},
Intel = { VisionRadius = 14 },
Expand Down
1 change: 1 addition & 0 deletions units/XRB0304/XRB0304_unit.bp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ UnitBlueprint{
ToggleCaps = { RULEUTC_ProductionToggle = true },
UnitName = "<LOC xrb0304_name>The Hive",
UpgradesFrom = "xrb0204",
UpgradesFromBase = "xrb0104",
},
Intel = { VisionRadius = 14 },
LifeBarHeight = 0.075,
Expand Down