Optimize electric network operations a little

This commit is contained in:
2025-01-08 22:45:12 +01:00
parent 6e2a3c22cf
commit 752fe4f192
5 changed files with 47 additions and 10 deletions

View File

@@ -116,6 +116,20 @@ function industrialtest.Machine.allowMetadataInventoryPut(self,pos,listname,inde
return industrialtest.Machine.allowMetadataInventoryPut(self,pos,listname,index,stack,player)
end
function industrialtest.ElectricMachine.requestPower(self,pos)
if not self.hasPowerOutput then
return
end
local meta=minetest.get_meta(pos)
if meta:get_int("industrialtest.powerAmount")<=0 then
return
end
local spaceAvailable,flowTransferred=industrialtest.api.powerFlow(pos)
if (spaceAvailable and meta:get_int("industrialtest.powerAmount")>0) or self:canUpdate(pos) then
minetest.get_node_timer(pos):start(industrialtest.updateDelay)
end
end
function industrialtest.ElectricMachine.powerExchange(self,pos)
local meta=minetest.get_meta(pos)
local shouldRerunTimer=false
@@ -124,7 +138,13 @@ function industrialtest.ElectricMachine.powerExchange(self,pos)
local networks=industrialtest.api.isAttachedToNetwork(meta)
if networks then
for _,network in ipairs(networks) do
minetest.get_node_timer(network):start(industrialtest.updateDelay)
local def=minetest.registered_nodes[minetest.get_node(network).name]
if def and def._industrialtest_self then
def._industrialtest_self:requestPower(network)
else
-- Support for bare definitions that don't use industrialtest pseudo-OOP
minetest.get_node_timer(network):start(industrialtest.updateDelay)
end
end
end
shouldRerunTimer=shouldRerunTimer or not industrialtest.api.isFullyCharged(meta)