From 83ee6fb4c8975361dfbe632258b36f662b110509 Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Fri, 25 Oct 2024 12:40:27 +0200 Subject: [PATCH] Fixes in ActivatedElectricMachine and ElectricMachine --- machines/activated_electric_machine.lua | 12 ++++--- machines/electric_machine.lua | 43 +++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 7 deletions(-) diff --git a/machines/activated_electric_machine.lua b/machines/activated_electric_machine.lua index 60d4daa..39a766a 100644 --- a/machines/activated_electric_machine.lua +++ b/machines/activated_electric_machine.lua @@ -17,14 +17,17 @@ industrialtest.ActivatedElectricMachine=table.copy(industrialtest.ElectricMachine) function industrialtest.ActivatedElectricMachine.onTimer(self,pos,elapsed) - industrialtest.ElectricMachine.requestPower(self,pos) - return industrialtest.ActivatedMachine.onTimer(self,pos,elapsed) + local result=self:powerExchange(pos) + local result2=industrialtest.ActivatedMachine.onTimer(self,pos,elapsed) + return result or result2 end industrialtest.ActivatedElectricMachine.register=industrialtest.ActivatedMachine.register industrialtest.ActivatedElectricMachine.createDefinitionTable=industrialtest.ActivatedMachine.createDefinitionTable +industrialtest.ActivatedElectricMachine.createActiveDefinitionTable=industrialtest.ActivatedMachine.createActiveDefinitionTable + industrialtest.ActivatedElectricMachine.shouldActivate=industrialtest.ActivatedMachine.shouldActivate industrialtest.ActivatedElectricMachine.shouldDeactivate=industrialtest.ActivatedMachine.shouldDeactivate @@ -34,7 +37,8 @@ industrialtest.ActivatedElectricMachine.afterActivation=industrialtest.Activated industrialtest.ActivatedElectricMachine.afterDeactivation=industrialtest.ActivatedMachine.afterDeactivation function industrialtest.ActivatedElectricMachine.activeOnTimer(self,pos,elapsed) - industrialtest.ElectricMachine.requestPower(self,pos) - return industrialtest.ActivatedMachine.activeOnTimer(self,pos,elapsed) + local result=self:powerExchange(pos) + local result2=industrialtest.ActivatedMachine.activeOnTimer(self,pos,elapsed) + return result or result2 end diff --git a/machines/electric_machine.lua b/machines/electric_machine.lua index ed7856e..bce4d7a 100644 --- a/machines/electric_machine.lua +++ b/machines/electric_machine.lua @@ -78,8 +78,8 @@ end function industrialtest.ElectricMachine.onTimer(self,pos,elapsed) local result=industrialtest.Machine.onTimer(self,pos,elapsed) - self:requestPower(pos) - return result + local result2=self:powerExchange(pos) + return result or result2 end function industrialtest.Machine.allowMetadataInventoryMove(self,pos,fromList,fromIndex,toList,toIndex,count) @@ -116,8 +116,10 @@ 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) +function industrialtest.ElectricMachine.powerExchange(self,pos) local meta=minetest.get_meta(pos) + local shouldRerunTimer=false + if self.hasPowerInput and not industrialtest.api.isFullyCharged(meta) then local networks=industrialtest.api.isAttachedToNetwork(meta) if networks then @@ -125,5 +127,40 @@ function industrialtest.ElectricMachine.requestPower(self,pos) minetest.get_node_timer(network):start(industrialtest.updateDelay) end end + shouldRerunTimer=shouldRerunTimer or not industrialtest.api.isFullyCharged(meta) end + + if self.hasPowerOutput and meta:get_int("industrialtest.powerAmount")>0 then + local spaceAvailable,flowTransferred=industrialtest.api.powerFlow(pos) + if flowTransferred then + self:updateFormspec(pos) + end + shouldRerunTimer=shouldRerunTimer or spaceAvailable + end + + if self.powerLists then + local inv=meta:get_inventory() + local powerFlow=meta:get_int("industrialtest.powerFlow") + for _,list in ipairs(self.powerLists) do + if meta:get_int("industrialtest.powerAmount")<=0 then + break + end + local slot=inv:get_stack(list,1) + if slot:get_count()>0 and not industrialtest.api.isFullyCharged(slot:get_meta()) then + industrialtest.api.transferPowerToItem(meta,slot,powerFlow) + inv:set_stack(list,1,slot) + self:updateFormspec(pos) + shouldRerunTimer=shouldRerunTimer or (not industrialtest.api.isFullyCharged(slot:get_meta()) and meta:get_int("industrialtest.powerAmount")>0) + end + end + end + + return shouldRerunTimer +end + +function industrialtest.ElectricMachine.createPowerIndicatorWidget(charged,x,y) + return table.concat({ + string.format("box[%f,%f;0.3,4.8;#202020]",x,y), + (charged>0 and string.format("box[%f,%f;0.3,%f;#FF1010]",x,y+4.8-(charged*4.8),charged*4.8) or ""), + },"") end