minetest-mods / technic

Technic mod for Minetest
Other
146 stars 155 forks source link

Battery Boxes don't work anymore behind Supply Converters #611

Closed debagos closed 1 year ago

debagos commented 1 year ago

After https://github.com/minetest-mods/technic/commit/718a5beda197cd0a775fd41aedfb5b1bbd5d1d8b Battery Boxes don't work anymore when they are behind a Supply Converter.

auouymous commented 1 year ago

577 even mentions they don't work...

SmallJoker commented 1 year ago

This does seem less of a problem compared to the previously wasted energy (supply surplus). I think this problem can be solved too by writing BA_eu_demand into the switching station metadata and using that as well for the power calculations.

I will have a look at this.


diff --git a/technic/machines/supply_converter.lua b/technic/machines/supply_converter.lua
index 826c94f..2ecd610 100644
--- a/technic/machines/supply_converter.lua
+++ b/technic/machines/supply_converter.lua
@@ -157,7 +157,8 @@ local run = function(pos, node, run_stage)
                        local demand = 0
                        if enabled then
                                -- Reverse evaluate the required machine and round to a nice number
-                               demand = 100 * math.ceil((sw_meta:get_int("demand") / efficiency) / 100)
+                               demand = sw_meta:get_int("ba_demand") + sw_meta:get_int("demand")
+                               demand = 100 * math.ceil(demand / efficiency / 100)
                                -- Do not draw more than the limit
                                demand = math.min(demand, meta:get_int("power"))
                        end
diff --git a/technic/machines/switching_station.lua b/technic/machines/switching_station.lua
index 907a6d1..bd34a0a 100644
--- a/technic/machines/switching_station.lua
+++ b/technic/machines/switching_station.lua
@@ -355,6 +355,8 @@ minetest.register_abm({
                        BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str)
                        BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str)
                end
+               -- Expose value for the supply converter
+               meta:set_int("ba_demand", BA_eu_demand)
                --dprint("Total BA supply:"..BA_eu_supply)
                --dprint("Total BA demand:"..BA_eu_demand)

I gave it a quick test but would like to hear your opinion on it before pushing @debagos . Does this diff solve your issue as you expect?

debagos commented 1 year ago

I added the patch and the battery boxes do work again, even without replacing them. Good job, thank you @SmallJoker for the quick response and fix! Should problems arise with that patch, then I'll report them here.

SmallJoker commented 1 year ago

705961e

Thank you. Feel free to leave a comment here if there's any issue with it. I will then reopen the issue.