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

feat(cmd_blueprint): add filtering for unbuildable blueprints #3861

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
109 changes: 109 additions & 0 deletions luaui/Widgets/Tests/cmd_blueprint/test_cmd_blueprint_filter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
local widgetName = "Blueprint"

function skip()
return Spring.GetGameFrame() <= 0
end

function setup()
assert(widgetHandler.knownWidgets[widgetName] ~= nil)

Test.clearMap()

initialWidgetActive = widgetHandler.knownWidgets[widgetName].active
if initialWidgetActive then
widgetHandler:DisableWidget(widgetName)
end
widgetHandler:EnableWidget(widgetName, true)
Comment on lines +12 to +16
Copy link
Collaborator

@saurtron saurtron Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can now replace this (lines 8, and 12 to 16) by Test.prepareWidget(widgetName). Also need to rebase or merge master to fix the same lines on test_cmd_blueprint_line.lua and test_cmd_blueprint_single.lua, they're already adapted in master. Check how it looks in master.


initialCameraState = Spring.GetCameraState()

Spring.SetCameraState({
mode = 5,
})
end

function cleanup()
Test.clearMap()

widgetHandler:DisableWidget(widgetName)
if initialWidgetActive then
widgetHandler:EnableWidget(widgetName, false)
end
Comment on lines +28 to +31
Copy link
Collaborator

@saurtron saurtron Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove lines 28-31 here, the widget restoring will be done automatically by the test runner when you use Test.prepareWidget(widgetName) at setup(). Alternatively you can call Test.restoreWidget(widgetName) instead of lines 28-31.


Spring.SetCameraState(initialCameraState)
end

local delay = 5
function test()
VFS.Include("luarules/configs/customcmds.h.lua")

widget = widgetHandler:FindWidget(widgetName)
assert(widget)

mock_saveBlueprintsToFile = Test.mock(widget, "saveBlueprintsToFile")

-- load test blueprints
widget.BLUEPRINT_FILE_PATH = "LuaUI/Widgets/Tests/cmd_blueprint/test_cmd_blueprint_filter_blueprints.json"
widget.loadBlueprintsFromFile()

Test.clearMap()

local builderUnitDefName = "armck"

local myTeamID = Spring.GetMyTeamID()
local x, z = Game.mapSizeX / 2, Game.mapSizeZ / 2
local y = Spring.GetGroundHeight(x, z)
local facing = 1

local builderUnitID = SyncedRun(function(locals)
return Spring.CreateUnit(
locals.builderUnitDefName,
locals.x,
locals.y,
locals.z,
locals.facing,
locals.myTeamID
)
end)

Spring.SelectUnit(builderUnitID)

Test.waitFrames(delay)

Spring.SetActiveCommand(
Spring.GetCmdDescIndex(CMD_BLUEPRINT_PLACE),
1,
true,
false,
false,
false,
false,
false
)

Test.waitFrames(delay)

assert(widget.blueprintPlacementActive)

-- make sure we skipped the blueprint #1, which is invalid
assert(widget.selectedBlueprintIndex == 2, widget.selectedBlueprintIndex)

-- make sure we skipped blueprint #3, which is cortex and unbuildable by armada
widget.handleBlueprintNextAction()
assert(widget.selectedBlueprintIndex == 4, widget.selectedBlueprintIndex)

-- make sure we rotate to the next valid blueprint, #2
widget.handleBlueprintDeleteAction()
assert(widget.selectedBlueprintIndex == 2, widget.selectedBlueprintIndex)

-- reset blueprints
widget.loadBlueprintsFromFile()

-- make sure we rotate to the next valid blueprint, #3 (which shifted from #4)
widget.handleBlueprintDeleteAction()
assert(widget.selectedBlueprintIndex == 3, widget.selectedBlueprintIndex)

-- make sure we rotate to the next valid blueprint, nil
widget.handleBlueprintDeleteAction()
assert(widget.selectedBlueprintIndex == nil, widget.selectedBlueprintIndex)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"savedBlueprints": [
{
"ordered": true,
"name": "",
"spacing": 0,
"facing": 0,
"units": [
{
"unitName": "INVALID",
"facing": 0,
"position": [0, 0, 0]
}
]
},
{
"ordered": true,
"name": "",
"spacing": 0,
"facing": 0,
"units": [
{
"unitName": "armwin",
"facing": 0,
"position": [0, 0, 0]
}
]
},
{
"ordered": true,
"name": "",
"spacing": 0,
"facing": 0,
"units": [
{
"unitName": "corwin",
"facing": 0,
"position": [0, 0, 0]
}
]
},
{
"ordered": true,
"name": "",
"spacing": 0,
"facing": 0,
"units": [
{
"unitName": "armwin",
"facing": 0,
"position": [-24, 0, 0]
},
{
"unitName": "corwin",
"facing": 0,
"position": [24, 0, 0]
}
]
}
]
}
5 changes: 2 additions & 3 deletions luaui/Widgets/Tests/cmd_blueprint/test_cmd_blueprint_line.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ function test()
widget = widgetHandler:FindWidget(widgetName)
assert(widget)

while #(widget.blueprints) > 0 do
widget.deleteBlueprint(1)
end
widget.blueprints = {}
widget.setSelectedBlueprintIndex(nil)

local blueprintUnitDefName = "armsolar"
local builderUnitDefName = "armck"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ function test()
widget = widgetHandler:FindWidget(widgetName)
assert(widget)

while #(widget.blueprints) > 0 do
widget.deleteBlueprint(1)
end
widget.blueprints = {}
widget.setSelectedBlueprintIndex(nil)

local blueprintUnitDefName = "armsolar"
local builderUnitDefName = "armck"
Expand Down
16 changes: 16 additions & 0 deletions luaui/Widgets/api_blueprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,21 @@ local function setActiveBuilders(unitIDs)
)
end

local function getBuildableUnits(blueprint)
local buildable = 0
local unbuildable = 0

for _, unit in ipairs(blueprint.units) do
if activeBuilderBuildOptions[unit.unitDefID] then
buildable = buildable + 1
else
unbuildable = unbuildable + 1
end
end

return buildable, unbuildable
end

function widget:Initialize()
if not gl.CreateShader then
-- no shader support, so just remove the widget itself, especially for headless
Expand All @@ -747,6 +762,7 @@ function widget:Initialize()
getBuildingDimensions = getBuildingDimensions,
getBlueprintDimensions = getBlueprintDimensions,
getUnitsBounds = getUnitsBounds,
getBuildableUnits = getBuildableUnits,
snapBlueprint = snapBlueprint,
BUILD_MODES = BUILD_MODES,
SQUARE_SIZE = SQUARE_SIZE,
Expand Down
Loading