1
0
mirror of https://github.com/metrostroi-repo/MetrostroiAddon.git synced 2026-05-02 00:42:29 +00:00

fix: optimize electric hook

This commit is contained in:
Ilya Krasnow
2026-01-18 02:28:55 +03:00
parent 2ef81e8e62
commit 6ba618e711

View File

@@ -496,10 +496,12 @@ local function consumeFromFeeder(inCurrent, inFeeder)
end
end
local prevTime
local C_Voltage = GetConVar("metrostroi_voltage")
local C_CurrentLimit = GetConVar("metrostroi_current_limit")
local prevTime = CurTime()
hook.Add("Think", "Metrostroi_ElectricConsumptionThink", function()
-- Change in time
prevTime = prevTime or CurTime()
local deltaTime = (CurTime() - prevTime)
prevTime = CurTime()
@@ -507,17 +509,28 @@ hook.Add("Think", "Metrostroi_ElectricConsumptionThink", function()
Metrostroi.TotalRateWatts = 0
Metrostroi.Current = 0
for k,v in pairs(Metrostroi.Currents) do Metrostroi.Currents[k] = 0 end
local bogeys = ents.FindByClass("gmod_train_bogey")
for _,bogey in pairs(bogeys) do
if bogey.Feeder then
Metrostroi.Currents[bogey.Feeder] = Metrostroi.Currents[bogey.Feeder] + bogey.DropByPeople
for train in pairs(Metrostroi.SpawnedTrains) do
if not train.Electric then continue end
local fB = IsValid(train.FrontBogey) and train.FrontBogey
if fB then
if fB.Feeder then
Metrostroi.Currents[fB.Feeder] = Metrostroi.Currents[fB.Feeder] + fB.DropByPeople
else
Metrostroi.Current = Metrostroi.Current + bogey.DropByPeople
Metrostroi.Current = Metrostroi.Current + fB.DropByPeople
end
end
for _,class in pairs(Metrostroi.TrainClasses) do
local trains = ents.FindByClass(class)
for _,train in pairs(trains) do
local rB = IsValid(train.RearBogey) and train.RearBogey
if rB then
if rB.Feeder then
Metrostroi.Currents[rB.Feeder] = Metrostroi.Currents[rB.Feeder] + rB.DropByPeople
else
Metrostroi.Current = Metrostroi.Current + rB.DropByPeople
end
end
end
for train in pairs(Metrostroi.SpawnedTrains) do
if train.Electric then
if train.Electric.EnergyChange then Metrostroi.TotalRateWatts = Metrostroi.TotalRateWatts + math.max(0, train.Electric.EnergyChange) end
local current = math.max(0, train.Electric.Itotal or 0) - math.max(0, train.Electric.Iexit or 0)
@@ -548,7 +561,6 @@ hook.Add("Think", "Metrostroi_ElectricConsumptionThink", function()
if not rBfeeder and not fBfeeder then consumeFromFeeder(current) end
end
end
end
-- Ignore invalid values
if Metrostroi.TotalRateWatts > 1e8 then Metrostroi.TotalRateWatts = 0 end
if Metrostroi.TotalRateWatts > 0 then
@@ -561,12 +573,12 @@ hook.Add("Think", "Metrostroi_ElectricConsumptionThink", function()
--Metrostroi.Current = Metrostroi.Current + Iperson
-- Check if exceeded global maximum current
if Metrostroi.Current > GetConVar("metrostroi_current_limit"):GetInt() then
if Metrostroi.Current > C_CurrentLimit:GetInt() then
Metrostroi.VoltageRestoreTimer = CurTime() + 7.0
print(Format("[!] Power feed protection tripped: current peaked at %.1f A",Metrostroi.Current))
end
local voltage = math.max(0,GetConVar("metrostroi_voltage"):GetInt())
local voltage = math.max(0,C_Voltage:GetInt())
-- Calculate new voltage
local Rfeed = 0.03 --25