Skip to content

Commit

Permalink
Enable cheat window to spawn multiple unit types at once (FAForever#3825
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Garanas authored Apr 27, 2022
1 parent 8837e39 commit 80c8243
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 44 deletions.
87 changes: 45 additions & 42 deletions lua/ui/dialogs/createunit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -685,55 +685,58 @@ function CreateDialog(x, y)

if SpawnThread then KillThread(SpawnThread) end

local function spreadSpawn(id, count, vet)

-- store selection so that units do not go of and try to build the unit we're
-- cheating in, is reset in EndCommandMode of '/lua/ui/game/commandmode.lua'
local selection = GetSelectedUnits()
SelectUnits(nil);

-- enables command mode for spawning units
import('/lua/ui/game/commandmode.lua').StartCommandMode(
"build",
{
-- default information required
name = id,

-- inform this is part of a cheat
cheat = true,

-- information for spawning
bpId = id,
count = tonumber(count:GetText()) or 1,
vet = tonumber(vet:GetText()) or 0,
yaw = (tonumber(orientation:GetText()) or 0) / 57.295779513,
army = currentArmy,
selection = selection,
}
)

-- options for user to exit the spawn mode
local function IsCancelKeyDown() return IsKeyDown('ESCAPE') or IsKeyDown(2) end

WaitSeconds(0.15)

-- check if user wants to exit
while not dialog do
if IsCancelKeyDown() then
import('/lua/ui/game/commandmode.lua').EndCommandMode(true)
break
local function spreadSpawn(ids, count, vet)

if table.getn(ids) > 0 then

-- store selection so that units do not go of and try to build the unit we're
-- cheating in, is reset in EndCommandMode of '/lua/ui/game/commandmode.lua'
local selection = GetSelectedUnits()
SelectUnits(nil);

-- enables command mode for spawning units
import('/lua/ui/game/commandmode.lua').StartCommandMode(
"build",
{
-- default information required
ids = ids,
index = 2,

-- inform this is part of a cheat
cheat = true,

-- information for spawning
name = ids[1],
bpId = ids[1],
count = tonumber(count:GetText()) or 1,
vet = tonumber(vet:GetText()) or 0,
yaw = (tonumber(orientation:GetText()) or 0) / 57.295779513,
army = currentArmy,
selection = selection,
}
)

-- options for user to exit the spawn mode
local function IsCancelKeyDown() return IsKeyDown('ESCAPE') or IsKeyDown(2) end

WaitSeconds(0.15)

-- check if user wants to exit
while not dialog do
if IsCancelKeyDown() then
import('/lua/ui/game/commandmode.lua').EndCommandMode(true)
break
end
WaitSeconds(0.1)
end
WaitSeconds(0.1)
end
end

local createBtn = UIUtil.CreateButtonStd(dialog, '/widgets/small', "Create", 12)
LayoutHelpers.AtBottomIn(createBtn, dialog)
LayoutHelpers.LeftOf(createBtn, cancelBtn, 5)
createBtn.OnClick = function(button)
for unitID, _ in CreationList do
SpawnThread = ForkThread(spreadSpawn, unitID, count, veterancyLevel)
end
ForkThread(spreadSpawn, table.keys(CreationList), count, veterancyLevel)
cancelBtn.OnClick()
end

Expand Down Expand Up @@ -1048,7 +1051,7 @@ function CreateDialog(x, y)
self:SetSolidColor(LineColors.Sel_Up)
end
elseif event.Type == 'ButtonDClick' and event.Modifiers.Left then
SpawnThread = ForkThread(spreadSpawn, self.unitID, count, veterancyLevel)
SpawnThread = ForkThread(spreadSpawn, { self.unitID }, count, veterancyLevel)
cancelBtn:OnClick()
elseif event.Type == 'MouseMotion' then
MoveMouseover(event.MouseX,event.MouseY)
Expand Down
21 changes: 19 additions & 2 deletions lua/ui/game/commandmode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,25 @@ function EndCommandMode(isCancel)
end

-- regain selection if we were cheating in units
if modeData.cheat and modeData.selection then
SelectUnits(modeData.selection)
if modeData.cheat then
if modeData.ids and modeData.index <= table.getn(modeData.ids) then
local modeData = table.deepcopy(modeData)
ForkThread(
function()
WaitSeconds(0.0001)

modeData.name = modeData.ids[modeData.index]
modeData.bpId = modeData.ids[modeData.index]
modeData.index = modeData.index + 1

StartCommandMode("build", modeData)
end
)
else
if modeData.selection then
SelectUnits(modeData.selection)
end
end
end

-- add information to modeData for end behavior
Expand Down

0 comments on commit 80c8243

Please sign in to comment.