Skip to content

Commit

Permalink
(#108) Remove first 3 meta read loops
Browse files Browse the repository at this point in the history
  • Loading branch information
SX authored and BuckarooBanzay committed Nov 21, 2020
1 parent 0f4ee25 commit dd021f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
74 changes: 30 additions & 44 deletions technic/machines/network.lua
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ function technic.add_network_branch(queue, network)
end
end

-- Battery charge status updates for network
local function update_battery(self, charge, max_charge, supply, demand)
self.battery_charge = self.battery_charge + charge
self.battery_charge_max = self.battery_charge_max + max_charge
self.battery_supply = self.battery_supply + supply
self.battery_demand = self.battery_demand + demand
if demand ~= 0 then
self.BA_count_active = self.BA_count_active + 1
self.BA_charge_active = self.BA_charge_active + charge
end
end

-- Moving average function generator
local function sma(period)
local values = {}
Expand Down Expand Up @@ -349,6 +361,9 @@ function technic.build_network(network_id)
PR_nodes = {}, RE_nodes = {}, BA_nodes = {},
-- Power generation, usage and capacity related variables
supply = 0, demand = 0, battery_charge = 0, battery_charge_max = 0,
BA_count_active = 0, BA_charge_active = 0, battery_supply = 0, battery_demand = 0,
-- Battery status update function
update_battery = update_battery,
-- Network activation and excution control
timeout = 0, skip = 0, lag = 0, average_lag = sma(5)
}
Expand All @@ -375,15 +390,15 @@ minetest.register_on_mods_loaded(function()
end
end)

local function run_nodes(list, vm, run_stage)
local function run_nodes(list, vm, run_stage, network)
for _, pos in ipairs(list) do
local node = minetest.get_node_or_nil(pos)
if not node then
vm:read_from_map(pos, pos)
node = minetest.get_node_or_nil(pos)
end
if node and node.name and node_technic_run[node.name] then
node_technic_run[node.name](pos, node, run_stage)
node_technic_run[node.name](pos, node, run_stage, network)
end
end
end
Expand Down Expand Up @@ -419,42 +434,28 @@ function technic.network_run(network_id)
return
end

-- Reset battery data for updates
network.battery_charge = 0
network.battery_charge_max = 0
network.battery_supply = 0
network.battery_demand = 0
network.BA_count_active = 0
network.BA_charge_active = 0

local vm = VoxelManip()
run_nodes(PR_nodes, vm, technic.producer)
run_nodes(RE_nodes, vm, technic.receiver)
run_nodes(BA_nodes, vm, technic.battery)
run_nodes(BA_nodes, vm, technic.battery, network)

-- Strings for the meta data
local eu_demand_str = tier.."_EU_demand"
local eu_input_str = tier.."_EU_input"
local eu_supply_str = tier.."_EU_supply"

-- Distribute charge equally across multiple batteries.
local charge_total = 0
local battery_count = 0

local BA_charge = 0
local BA_charge_max = 0

for n, pos1 in pairs(BA_nodes) do
local meta1 = minetest.get_meta(pos1)
local charge = meta1:get_int("internal_EU_charge")
local charge_max = meta1:get_int("internal_EU_charge_max")

BA_charge = BA_charge + charge
BA_charge_max = BA_charge_max + charge_max

if (meta1:get_int(eu_demand_str) ~= 0) then
charge_total = charge_total + charge
battery_count = battery_count + 1
end
end

local charge_distributed = math.floor(charge_total / battery_count)

local charge_distributed = math.floor(network.BA_charge_active / network.BA_count_active)
for n, pos1 in pairs(BA_nodes) do
local meta1 = minetest.get_meta(pos1)

if (meta1:get_int(eu_demand_str) ~= 0) then
meta1:set_int("internal_EU_charge", charge_distributed)
end
Expand All @@ -476,22 +477,6 @@ function technic.network_run(network_id)
end
--dprint("Total RE demand:"..RE_eu_demand)

-- Get all the power from the BA nodes
local BA_eu_supply = 0
for _, pos1 in pairs(BA_nodes) do
local meta1 = minetest.get_meta(pos1)
BA_eu_supply = BA_eu_supply + meta1:get_int(eu_supply_str)
end
--dprint("Total BA supply:"..BA_eu_supply)

-- Get all the demand from the BA nodes
local BA_eu_demand = 0
for _, pos1 in pairs(BA_nodes) do
local meta1 = minetest.get_meta(pos1)
BA_eu_demand = BA_eu_demand + meta1:get_int(eu_demand_str)
end
--dprint("Total BA demand:"..BA_eu_demand)

technic.network_infotext(network_id, S("@1. Supply: @2 Demand: @3",
S("Switching Station"), technic.EU_string(PR_eu_supply),
technic.EU_string(RE_eu_demand)))
Expand All @@ -514,10 +499,9 @@ function technic.network_run(network_id)
network.supply = PR_eu_supply
network.demand = RE_eu_demand
network.battery_count = #BA_nodes
network.battery_charge = BA_charge
network.battery_charge_max = BA_charge_max

-- If the PR supply is enough for the RE demand supply them all
local BA_eu_demand = network.battery_demand
if PR_eu_supply >= RE_eu_demand then
--dprint("PR_eu_supply"..PR_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
for _, pos1 in pairs(RE_nodes) do
Expand All @@ -532,6 +516,7 @@ function technic.network_run(network_id)
if BA_eu_demand > 0 then
charge_factor = PR_eu_supply / BA_eu_demand
end
-- TODO: EU input for all batteries: math.floor(BA_eu_demand * charge_factor * #BA_nodes)
for n, pos1 in pairs(BA_nodes) do
local meta1 = minetest.get_meta(pos1)
local eu_demand = meta1:get_int(eu_demand_str)
Expand All @@ -548,6 +533,7 @@ function technic.network_run(network_id)
end

-- If the PR supply is not enough for the RE demand we will discharge the batteries too
local BA_eu_supply = network.battery_supply
if PR_eu_supply + BA_eu_supply >= RE_eu_demand then
--dprint("PR_eu_supply "..PR_eu_supply.."+BA_eu_supply "..BA_eu_supply.." >= RE_eu_demand"..RE_eu_demand)
for _, pos1 in pairs(RE_nodes) do
Expand Down
12 changes: 7 additions & 5 deletions technic/machines/register/battery_box.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function technic.register_battery_box(data)
return batt_charge + charge_step, (tool_charge == 0)
end

local function run(pos, node)
local function run(pos, node, run_state, network)
local meta = minetest.get_meta(pos)

local eu_input = meta:get_int(tier.."_EU_input")
Expand Down Expand Up @@ -292,10 +292,12 @@ function technic.register_battery_box(data)
end

-- We allow batteries to charge on less than the demand
meta:set_int(tier.."_EU_demand",
math.min(data.charge_rate, max_charge - current_charge))
meta:set_int(tier.."_EU_supply",
math.min(data.discharge_rate, current_charge))
local supply = math.min(data.discharge_rate, current_charge)
local demand = math.min(data.charge_rate, max_charge - current_charge)
network:update_battery(current_charge, max_charge, supply, demand)

meta:set_int(tier.."_EU_demand", demand)
meta:set_int(tier.."_EU_supply", supply)
meta:set_int("internal_EU_charge", current_charge)
meta:set_int("internal_EU_charge_max", max_charge)

Expand Down

0 comments on commit dd021f6

Please sign in to comment.