Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: unit_estall_disable.lua
-- brief: disables Units during energy stall
-- author: Argh
--
-- Copyright (C) 2007.
-- Licensed under the terms of the GNU GPL, v2 or later. Based on GPL code written by Licho for CA mod.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadget:GetInfo()
return {
name = "UnitEstall",
desc = "Deactivates Units during energy stall",
author = "Argh",
date = "30.7.2007",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true -- loaded by default?
}
end
--------------------------------------------------------------------------------
if (gadgetHandler:IsSyncedCode()) then
--------------------------------------------------------------------------------
-- SYNCED
--------------------------------------------------------------------------------
--Speed-ups
local insert = table.insert
local GiveOrderToUnit = Spring.GiveOrderToUnit
local GetUnitStates = Spring.GetUnitStates
local GetUnitTeam = Spring.GetUnitTeam
local GetUnitResources = Spring.GetUnitResources
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local units = {}
local disabledUnits = {}
local canShutDownDefs = {
-------------------------------
----------------------------------------insert your Units into this list here, just dummy code
[ UnitDefNames['yourUnitName'].id ] = true,
----------------------------------------
--------------------------------
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadget:Initialize()
for _,unitID in ipairs(Spring.GetAllUnits()) do
local unitDefID = Spring.GetUnitDefID(unitID)
AddUnit(unitID, unitDefID)
end
end
function AddUnit(unitID, unitDefID)
if (canShutDownDefs[unitDefID]) then
units[unitID] = unitDefID
end
end
function gadget:UnitCreated(unitID, unitDefID)
AddUnit(unitID, unitDefID)
end
function gadget:UnitTaken(unitID, unitDefID)
AddUnit(unitID, unitDefID)
end
function gadget:UnitDestroyed(unitID)
units[unitID] = nil
end
function gadget:GameFrame(n)
if ((n % 61) < 0.1) then
local teamEnergy = {}
local temp = Spring.GetTeamList()
for _,teamID in ipairs(temp) do
local eCur, eMax, eUse, eInc, _, _, _, eRec = Spring.GetTeamResources(teamID, "energy")
teamEnergy[teamID] = eCur - eUse + eInc
end
for unitID,unitDefID in pairs(units) do
local _, _, _, eu = GetUnitResources(unitID)
local disabledUnitEnergyUse = disabledUnits[unitID]
if (disabledUnitEnergyUse~=nil) then -- we have disabled unit
local unitTeamID = GetUnitTeam(unitID)
if (disabledUnitEnergyUse < teamEnergy[unitTeamID]) then -- we still have enough energy to reenable unit
disabledUnits[unitID]=nil
GiveOrderToUnit(unitID, CMD.ONOFF, { 1 }, { }) -- turns Units back ON and sends Activatesc() function call to COB
Spring.SetUnitHealth(unitID, { paralyze = -1}) -- turns Units back on again
teamEnergy[unitTeamID] = teamEnergy[unitTeamID] - disabledUnitEnergyUse
end
else -- we have non-disabled unit
if (eu == 0) then -- there is not enough energy to keep unit running (its energy use auto dropped to 0), we will disable it
if (GetUnitStates(unitID).active) then -- only disable "active" unit
GiveOrderToUnit(unitID, CMD.ONOFF, { 0 }, { }) -- turns Units OFF and sends Deactivatesc() function call to COB
Spring.SetUnitHealth(unitID, { paralyze = 1.0e9 }) -- turns Units off
disabledUnits[unitID] = UnitDefs[unitDefID].energyUpkeep
end
end
end
end
end
end
function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
if (cmdID == CMD.ONOFF and disabledUnits[unitID]~=nil) then
return false
else
return true
end
end
--------------------------------------------------------------------------------
-- END SYNCED
--------------------------------------------------------------------------------
end
1. I think running it every game frame is excessive. I understand that the code section here:
Code: Select all
function gadget:GameFrame(n)
if ((n % 61) < 0.1) then
2. I want to shut down everything that's defined in my list, if Energy goes below X level, not just a few things, based on where in the stack they are, and how much Energy is still present. This is because of Spring's stupid, "I can't go below zero" economy, which I've complained at length about elsewhere. Is there any way to do this? I can live with it waiting another SlowUpdate before getting whatever % of Units didn't qualify, but I want everything to come to a screeching halt until Energy levels rise above X level again, and stay there. The way that this script works now, only about 60-75% of the Units will shut down, even if I set up an incredibly-large Energy drain! That's unacceptable.
3. Having tested this script, it works great... unless you CTRL-Z all of the stunned Units. Then it results in massive slowdown and lots of hard-drive churning! I think this is because it's the code for replays again, recording all of the ON/OFF states as a "command" through the UI, and it's incredibly annoying. Is there any way to shut this off? I mean, I obviously must run this code synched... is there a way to tell the Replay recorder to ignore this "command" from the UI?
Um, that's about it. I'd imagine that fixing the timing's the easy one, but any help I can get for the other two issues would be much-appreciated.