📄 unit_metal_maker.lua
字号:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------- file: unit_metal_maker.lua-- brief: controls metal makers to minimize energy stalls-- author: Dave Rodgers---- Copyright (C) 2007.-- Licensed under the terms of the GNU GPL, v2 or later.------------------------------------------------------------------------------------------------------------------------------------------------------------------function widget:GetInfo() return { name = "MetalMakers", desc = "Controls metal makers to minimize energy stalls", author = "trepan", date = "Jan 8, 2007", license = "GNU GPL, v2 or later", layer = 0, enabled = false -- loaded by default? }end------------------------------------------------------------------------------------------------------------------------------------------------------------------ Automatically generated local definitionslocal CMD_ONOFF = CMD.ONOFFlocal spGetMyTeamID = Spring.GetMyTeamIDlocal spGetTeamResources = Spring.GetTeamResourceslocal spGetTeamUnits = Spring.GetTeamUnitslocal spGetUnitDefID = Spring.GetUnitDefIDlocal spGiveOrderToUnitMap = Spring.GiveOrderToUnitMap----------------------------------------------------------------------------------------------------------------------------------------------------------------local ON_METAL_LIMIT = 0.8local OFF_METAL_LIMIT = 0.9local ON_ENERGY_LIMIT = 0.7local OFF_ENERGY_LIMIT = 0.3local metalMakers = {}local currentState = true--------------------------------------------------------------------------------local function SetMetalMakers(state) currentState = state local numState = currentState and 1 or 0 spGiveOrderToUnitMap(metalMakers, CMD_ONOFF, { numState }, {} )end--------------------------------------------------------------------------------function widget:Initialize() local units = spGetTeamUnits(spGetMyTeamID()) for _,uid in ipairs(units) do local udid = spGetUnitDefID(uid) local ud = UnitDefs[udid] if (ud.isMetalMaker) then metalMakers[uid] = true print("Added metal maker: "..uid) end end SetMetalMakers(true)endfunction widget:Update(deltaTime) local mNow, mMax = spGetTeamResources(spGetMyTeamID(), "metal") local eNow, eMax = spGetTeamResources(spGetMyTeamID(), "energy") local mFrac = (mNow / mMax) local eFrac = (eNow / eMax) if (currentState) then if ((eFrac < OFF_ENERGY_LIMIT) or (mFrac > OFF_METAL_LIMIT)) then SetMetalMakers(false) end else if ((eFrac > ON_ENERGY_LIMIT) and (mFrac < ON_METAL_LIMIT)) then SetMetalMakers(true) end endendfunction widget:UnitFinished(unitID, unitDefID, unitTeam) if (unitTeam ~= spGetMyTeamID()) then return end local ud = UnitDefs[unitDefID] if (ud.isMetalMaker) then print("Added metal maker: "..unitID) metalMakers[unitID] = true -- set the new unit to our currentState local tmpMakers = metalMakers metalMakers = { [unitID] = true } SetMetalMakers(currentState) metalMakers = tmpMakers endendfunction widget:UnitDestroyed(unitID, unitDefID, unitTeam) if (metalMakers[unitID]) then print("Removed metal maker: "..unitID) end metalMakers[unitID] = nilendfunction widget:UnitTaken(unitID, unitDefID, unitTeam, newTeam) widget:UnitDestroyed(unitID, unitDefID)endfunction widget:UnitGiven(unitID, unitDefID, unitTeam, oldTeam) widget:UnitFinished(unitID, unitDefID, unitTeam)end----------------------------------------------------------------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -