Skip to content

Commit

Permalink
Engineer:
Browse files Browse the repository at this point in the history
 * Add copy blueprint command and UI.
 * Reverted speed back to 36 since this seems interesting enough.
  • Loading branch information
GoogleFrog committed Apr 2, 2024
1 parent efcde0b commit fac2c05
Show file tree
Hide file tree
Showing 12 changed files with 423 additions and 27 deletions.
2 changes: 2 additions & 0 deletions Anims/cursorfacselect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hotspot topleft
frame cursorfacselect_0.png 5
Binary file added Anims/cursorfacselect_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions LuaRules/Configs/customcmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ return {
WANTED_SPEED = 38825,
AIR_STRAFE = 39381,
AIR_MANUALFIRE = 38571,
FIELD_FAC_SELECT = 38693,
FIELD_FAC_UNIT_TYPE = 38694,

-- terraform
RAMP = 39734,
Expand Down
216 changes: 196 additions & 20 deletions LuaRules/Gadgets/unit_field_factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,43 @@ function gadget:GetInfo()
}
end

local fieldFacDefID = {}
local CMD_FIELD_FAC_SELECT = Spring.Utilities.CMD.FIELD_FAC_SELECT
local CMD_FIELD_FAC_UNIT_TYPE = Spring.Utilities.CMD.FIELD_FAC_UNIT_TYPE

if (gadgetHandler:IsSyncedCode()) then

--------------------------------------------------------------------------------
-- SYNCED
--------------------------------------------------------------------------------

local facSelectCmd = {
id = CMD_FIELD_FAC_SELECT,
type = CMDTYPE.ICON_UNIT,
cursor = 'facselect',
action = 'field_fac_select',
name = 'Factory Select',
params = { },
hidden = false,
}

local canBuild = {}
local isFieldFac = {}
local nextDesiredUnitType = {}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Build option adding and removal.

local fieldFacRange = {}
for unitDefID = 1, #UnitDefs do
local ud = UnitDefs[unitDefID]
if ud.customParams.field_factory then
fieldFacDefID[unitDefID] = true
fieldFacRange[unitDefID] = ud.buildDistance
end
end

local ALLY_TABLE = {ally = true}

local factories = {
[[factoryshield]],
[[factorycloak]],
Expand All @@ -31,16 +60,45 @@ local factories = {
[[factorytank]],
[[factoryship]],
[[striderhub]],
[[plateshield]],
[[platecloak]],
[[plateveh]],
[[plateplane]],
[[plategunship]],
[[platehover]],
[[plateamph]],
[[platespider]],
[[platejump]],
[[platetank]],
[[plateship]],
}

local buildParams = {
type = 20,
action = "buildunit_etc",
id = -1,
tooltip = "",
cursor = "etc",
showUnique = false,
params = {},
name = "etc",
onlyTexture = false,
disabled = false,
hidden = false,
queueing = true,
texture = "",
}

local removedCmdDesc = {}
local factoryDefIDs = {}
local fieldBuildOpts = {}
do
local alreadyAdded = {}
for i = 1, #factories do
local factoryName = factories[i]
if UnitDefNames[factoryName] then
local buildList = UnitDefNames[factoryName].buildOptions
local ud = UnitDefNames[factoryName]
if ud then
factoryDefIDs[ud.id] = true
local buildList = ud.buildOptions
for j = 1, #buildList do
local buildDefID = buildList[j]
if not alreadyAdded[buildDefID] then
Expand All @@ -52,16 +110,9 @@ do
end
end

local canBuild = {}
local isFieldFac = {}

local function RemoveUnit(unitID, lockDefID)
local cmdDescID = Spring.FindUnitCmdDesc(unitID, -lockDefID)
if (cmdDescID) then
if not removedCmdDesc[lockDefID] then
local toRemove = Spring.GetUnitCmdDescs(unitID, cmdDescID, cmdDescID)
removedCmdDesc[lockDefID] = toRemove[1]
end
Spring.RemoveUnitCmdDesc(unitID, cmdDescID)
if canBuild[unitID] and canBuild[unitID] == lockDefID then
canBuild[unitID] = nil
Expand All @@ -71,12 +122,107 @@ end

local function AddUnit(unitID, lockDefID)
local cmdDescID = Spring.FindUnitCmdDesc(unitID, -lockDefID)
if (not cmdDescID) and removedCmdDesc[lockDefID] then
Spring.InsertUnitCmdDesc(unitID, removedCmdDesc[lockDefID])
if (not cmdDescID) then
local name = UnitDefs[lockDefID].name
buildParams.id = -lockDefID
buildParams.cursor = name
buildParams.name = name
buildParams.action = "buildunit_name"
Spring.InsertUnitCmdDesc(unitID, buildParams)
canBuild[unitID] = lockDefID
Spring.SetUnitRulesParam(unitID, "fieldFactoryUnit", lockDefID, ALLY_TABLE)
end
end

local function FactoryCanBuild(unitID, unitDefID)
local cmdDescID = Spring.FindUnitCmdDesc(unitID, -unitDefID)
if not cmdDescID then
return true, true
end
if (GG.att_EconomyChange[unitID] or 1) <= 0 then
return true, false
end
local stunnedOrInbuild = Spring.GetUnitIsStunned(unitID)
return stunnedOrInbuild, false
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Command handling

function gadget:AllowCommand_GetWantedCommand()
return {[CMD_FIELD_FAC_SELECT] = true, [CMD_FIELD_FAC_UNIT_TYPE] = true}
end

function gadget:AllowCommand_GetWantedUnitDefID()
return fieldFacRange
end

function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
if not fieldFacRange[unitDefID] then
return true
end
if cmdID == CMD_FIELD_FAC_UNIT_TYPE then
nextDesiredUnitType[unitID] = cmdParams and cmdParams[1]
return false
end
if cmdID == CMD_FIELD_FAC_SELECT then
if not cmdParams and cmdParams[1] and Spring.ValidUnitID(cmdParams[1]) then
return false
end
if not Spring.AreTeamsAllied(teamID, Spring.GetUnitTeam(cmdParams[1])) then
return false
end
nextDesiredUnitType[unitID] = false
local targetDefID = Spring.GetUnitDefID(cmdParams[1])
return targetDefID and factoryDefIDs[targetDefID]
end

return true
end

function gadget:CommandFallback(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
if cmdID ~= CMD_FIELD_FAC_SELECT then
return false
end

local targetID = cmdParams and cmdParams[1]
if not Spring.ValidUnitID(targetID) then
Spring.ClearUnitGoal(unitID)
return true, true
end
local x, y, z = Spring.GetUnitPosition(targetID)
if not z then
Spring.ClearUnitGoal(unitID)
return true, true
end
if nextDesiredUnitType[unitID] then
local temporaryProblem, permanentProblem = FactoryCanBuild(targetID, nextDesiredUnitType[unitID])
if permanentProblem then
Spring.ClearUnitGoal(unitID)
return true, true
end
local distance = Spring.GetUnitSeparation(unitID, targetID, true)
if distance <= fieldFacRange[unitDefID] and not temporaryProblem then
if canBuild[unitID] ~= nextDesiredUnitType[unitID] then
if canBuild[unitID] then
RemoveUnit(unitID, canBuild[unitID])
end
AddUnit(unitID, nextDesiredUnitType[unitID])
end
nextDesiredUnitType[unitID] = nil
Spring.ClearUnitGoal(unitID)
return true, true
end
end
Spring.SetUnitMoveGoal(unitID, x, y, z, fieldFacRange[unitDefID] - 16)
return true, false
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- API

function GG.FieldConstruction_NotifyPlop(unitID, factoryID, factoryDefID)
if not factoryDefID then
return
Expand All @@ -88,26 +234,56 @@ function GG.FieldConstruction_NotifyPlop(unitID, factoryID, factoryDefID)
AddUnit(unitID, buildList[2])
end

function gadget:AllowUnitCreation(unitDefID, builderID, builderTeamID, x, y, z)
if not isFieldFac[builderID] then
return true
end
return (not fieldBuildOpts[unitDefID]) or (canBuild[builderID] == unitDefID)
end

function gadget:UnitCreated(unitID, unitDefID)
if not fieldFacDefID[unitDefID] then
if not fieldFacRange[unitDefID] then
return
end
isFieldFac[unitID] = true
Spring.InsertUnitCmdDesc(unitID, facSelectCmd)
local previousUnit = Spring.GetUnitRulesParam(unitID, "fieldFactoryUnit")
for i = 1, #fieldBuildOpts do
RemoveUnit(unitID, fieldBuildOpts[i])
end
if previousUnit then
AddUnit(unitID, previousUnit)
end
end

function gadget:UnitDestroyed(unitID, unitDefID)
if not fieldFacDefID[unitDefID] then
if not fieldFacRange[unitDefID] then
return
end
isFieldFac[unitID] = nil
end

function gadget:AllowUnitCreation(unitDefID, builderID, builderTeamID, x, y, z)
if not isFieldFac[builderID] then
return true
function gadget:Initialize()
gadgetHandler:RegisterCMDID(CMD_FIELD_FAC_SELECT)
local allUnits = Spring.GetAllUnits()
for i=1,#allUnits do
local unitID = allUnits[i]
local unitDefID = Spring.GetUnitDefID(unitID)
gadget:UnitCreated(unitID, unitDefID)
end
return (not fieldBuildOpts[unitDefID]) or (canBuild[builderID] == unitDefID)
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
else
--------------------------------------------------------------------------------
-- UNSYNCED
--------------------------------------------------------------------------------

function gadget:Initialize()
gadgetHandler:RegisterCMDID(CMD_FIELD_FAC_SELECT)
Spring.SetCustomCommandDrawData(CMD_FIELD_FAC_SELECT, "FactorySelect", {0.2, 0.7, 1.0, 0.7})
Spring.AssignMouseCursor("facselect", "cursorfacselect", true, true)
end

end
2 changes: 2 additions & 0 deletions LuaUI/Configs/customCmdTypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ local custom_cmd_actions = {
jump = {cmdType = 1, name = "Jump"},
find_pad = {cmdType = 3, name = "Return to Airbase"},
exclude_pad = {cmdType = 1, name = "Exclude Airpad"},
field_fac_select = {cmdType = 1, name = "Copy Factory Blueprint"},
embark = {cmdType = 3, name = "Embark"},
disembark = {cmdType = 3, name = "Disembark"},
loadselected = {cmdType = 3, name = "Load Selected Units"},
Expand Down Expand Up @@ -202,6 +203,7 @@ local usedActions = {
["setferry"] = true,
["sethaven"] = true,
["exclude_pad"] = true,
["field_fac_select"] = true,
["setfirezone"] = true,
["cancelfirezone"] = true,
["selection_rank"] = true,
Expand Down
11 changes: 6 additions & 5 deletions LuaUI/Configs/integral_menu_commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ local cmdPosDef = {
[CMD.UNLOAD_UNITS] = {pos = 7, priority = 8},
[CMD_RECALL_DRONES] = {pos = 7, priority = 10},

[CMD_AREA_TERRA_MEX]= {pos = 13, priority = 1},
[CMD_FIELD_FAC_SELECT] = {pos = 13, priority = 0.6},
[CMD_MISC_BUILD] = {pos = 13, priority = 0.7},
[CMD_AREA_TERRA_MEX] = {pos = 13, priority = 1},
[CMD_UNIT_SET_TARGET_CIRCLE] = {pos = 13, priority = 2},
[CMD_UNIT_CANCEL_TARGET] = {pos = 13, priority = 3},
[CMD_EMBARK] = {pos = 13, priority = 5},
[CMD_DISEMBARK] = {pos = 13, priority = 6},
[CMD_EXCLUDE_PAD] = {pos = 13, priority = 7},
[CMD_MISC_BUILD] = {pos = 13, priority = 7},
[CMD_EMBARK] = {pos = 13, priority = 5},
[CMD_DISEMBARK] = {pos = 13, priority = 6},
[CMD_EXCLUDE_PAD] = {pos = 13, priority = 7},

-- States
[CMD.REPEAT] = {pos = 1, priority = 1},
Expand Down
1 change: 1 addition & 0 deletions LuaUI/Configs/integral_menu_config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ local commandDisplayConfig = {
[CMD_FIND_PAD] = {texture = imageDir .. 'Bold/rearm.png', tooltip = "Resupply: Return to nearest Airpad for repairs and, for bombers, ammo."},

[CMD_EXCLUDE_PAD] = {texture = imageDir .. 'Bold/excludeairpad.png', tooltip = "Exclude Airpad: Toggle whether any of your aircraft use the targeted airpad."},
[CMD_FIELD_FAC_SELECT] = {texture = imageDir .. 'Bold/fac_select.png', tooltip = "Copy Factory Blueprint: Copy a production option from target functional friendly factory."},

[CMD_EMBARK] = {texture = imageDir .. 'Bold/embark.png'},
[CMD_DISEMBARK] = {texture = imageDir .. 'Bold/disembark.png'},
Expand Down
1 change: 1 addition & 0 deletions LuaUI/Configs/integral_menu_culling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local configList = {
{cmdID = CMD_AREA_MEX , default = true, name = "Area Mex"},
{cmdID = CMD.FIGHT , default = true, name = "Attack Move"},
{cmdID = CMD_BUILD_PLATE , default = true, name = "Build Plate"},
{cmdID = CMD_FIELD_FAC_SELECT , default = true, name = "Copy Factory Blueprint"},
{cmdID = CMD_EMBARK , default = true, name = "Embark"},
{cmdID = CMD.MANUALFIRE , default = true, name = "Fire Special Weapon"},
{cmdID = CMD_AIR_MANUALFIRE , default = true, name = "Fire Special Weapon (Aircraft)"},
Expand Down
Binary file added LuaUI/Images/commands/Bold/fac_select.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fac2c05

Please sign in to comment.