Added the Mv and HV down converters.

Doing that required some changes to the batteries in that they have new meta data
aligned with the other machines.
I also added a BA type machine in addition to the existing PR and RE types.
This way it is easy to find he batteries on the network.
This commit is contained in:
kpoppel 2013-06-12 23:30:37 +02:00
parent a27b59dde5
commit cfb5bd190c
13 changed files with 1371 additions and 975 deletions

@ -1,11 +1,7 @@
LV_machines = {} -- register LV machines here
registered_LV_machines_count=0 local LV_machines = {}
function register_LV_machine(string1,string2) function register_LV_machine(string1,string2)
registered_LV_machines_count=registered_LV_machines_count+1 LV_machines[string1] = string2
LV_machines[registered_LV_machines_count]={}
LV_machines[registered_LV_machines_count].machine_name=string1
LV_machines[registered_LV_machines_count].machine_type=string2
end end
power_tools ={} power_tools ={}
@ -18,7 +14,6 @@ power_tools[registered_power_tools_count].tool_name=string1
power_tools[registered_power_tools_count].max_charge=max_charge power_tools[registered_power_tools_count].max_charge=max_charge
end end
register_power_tool ("technic:battery",10000) register_power_tool ("technic:battery",10000)
register_power_tool ("technic:red_energy_crystal",100000) register_power_tool ("technic:red_energy_crystal",100000)
register_power_tool ("technic:green_energy_crystal",250000) register_power_tool ("technic:green_energy_crystal",250000)
@ -67,28 +62,28 @@ battery_box_formspec =
"label[1,3;Power level]".. "label[1,3;Power level]"..
"list[current_player;main;0,5;8,4;]" "list[current_player;main;0,5;8,4;]"
minetest.register_node("technic:battery_box", { minetest.register_node(
description = "Battery box", "technic:battery_box", {
description = "LV Battery Box",
tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.png", "technic_battery_box_side0.png", tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.png", "technic_battery_box_side0.png",
"technic_battery_box_side0.png", "technic_battery_box_side0.png", "technic_battery_box_side0.png"}, "technic_battery_box_side0.png", "technic_battery_box_side0.png", "technic_battery_box_side0.png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
technic_power_machine=1,
last_side_shown=0,
drop="technic:battery_box", drop="technic:battery_box",
on_construct = function(pos) on_construct = function(pos)
if pos == nil then return end
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
meta:set_string("infotext", "Battery box") meta:set_string("infotext", "Battery box")
meta:set_float("technic_power_machine", 1) meta:set_float("technic_power_machine", 1)
meta:set_string("formspec", battery_box_formspec) meta:set_string("formspec", battery_box_formspec)
local inv = meta:get_inventory() meta:set_float("internal_EU_buffer", 0)
meta:set_float("internal_EU_buffer_size", 60000)
inv:set_size("src", 1) inv:set_size("src", 1)
inv:set_size("dst", 1) inv:set_size("dst", 1)
battery_charge = 0
max_charge = 60000
last_side_shown=0
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
if pos == nil then return end
local meta = minetest.env:get_meta(pos); local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory() local inv = meta:get_inventory()
if not inv:is_empty("dst") then if not inv:is_empty("dst") then
@ -102,28 +97,28 @@ minetest.register_node("technic:battery_box", {
for i=1,8,1 do for i=1,8,1 do
minetest.register_node("technic:battery_box"..i, { minetest.register_node(
description = "Battery box", "technic:battery_box"..i, {
description = "LV Battery Box",
tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.png", "technic_battery_box_side0.png^technic_power_meter"..i..".png", tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.png", "technic_battery_box_side0.png^technic_power_meter"..i..".png",
"technic_battery_box_side0.png^technic_power_meter"..i..".png", "technic_battery_box_side0.png^technic_power_meter"..i..".png", "technic_battery_box_side0.png^technic_power_meter"..i..".png"}, "technic_battery_box_side0.png^technic_power_meter"..i..".png", "technic_battery_box_side0.png^technic_power_meter"..i..".png", "technic_battery_box_side0.png^technic_power_meter"..i..".png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
technic_power_machine=1,
last_side_shown=0,
drop="technic:battery_box", drop="technic:battery_box",
on_construct = function(pos) on_construct = function(pos)
if pos == nil then return end
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
meta:set_string("infotext", "Battery box") meta:set_string("infotext", "Battery box")
meta:set_float("technic_power_machine", 1) meta:set_float("technic_power_machine", 1)
meta:set_string("formspec", battery_box_formspec) meta:set_string("formspec", battery_box_formspec)
local inv = meta:get_inventory() meta:set_float("internal_EU_buffer", 0)
meta:set_float("internal_EU_buffer_size", 60000)
inv:set_size("src", 1) inv:set_size("src", 1)
inv:set_size("dst", 1) inv:set_size("dst", 1)
battery_charge = 0
max_charge = 60000
last_side_shown=0
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
if pos == nil then return end
local meta = minetest.env:get_meta(pos); local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory() local inv = meta:get_inventory()
if not inv:is_empty("dst") then if not inv:is_empty("dst") then
@ -136,8 +131,6 @@ minetest.register_node("technic:battery_box"..i, {
}) })
end end
LV_nodes_visited = {}
function get_RE_item_load (load1,max_load) function get_RE_item_load (load1,max_load)
if load1==0 then load1=65535 end if load1==0 then load1=65535 end
local temp = 65536-load1 local temp = 65536-load1
@ -158,25 +151,27 @@ item_stack["wear"]=tostring(temp)
return item_stack return item_stack
end end
minetest.register_abm({ minetest.register_abm(
nodenames = {"technic:battery_box","technic:battery_box1","technic:battery_box2","technic:battery_box3","technic:battery_box4", {nodenames = {"technic:battery_box","technic:battery_box1","technic:battery_box2","technic:battery_box3","technic:battery_box4",
"technic:battery_box5","technic:battery_box6","technic:battery_box7","technic:battery_box8"}, "technic:battery_box5","technic:battery_box6","technic:battery_box7","technic:battery_box8"},
interval = 1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
charge= meta:get_int("battery_charge") local max_charge = 60000 -- Set maximum charge for the device here
max_charge= 60000 local charge = meta:get_int("internal_EU_buffer")
-- Select node textures
local i=math.ceil((charge/max_charge)*8) local i=math.ceil((charge/max_charge)*8)
if i > 8 then i = 8 end if i > 8 then i = 8 end
j=meta:get_float("last_side_shown") local j = meta:get_float("last_side_shown")
if i~=j then if i~=j then
if i>0 then hacky_swap_node(pos,"technic:battery_box"..i) if i>0 then hacky_swap_node(pos,"technic:battery_box"..i)
elseif i==0 then hacky_swap_node(pos,"technic:battery_box") end elseif i==0 then hacky_swap_node(pos,"technic:battery_box") end
meta:set_float("last_side_shown",i) meta:set_float("last_side_shown",i)
end end
--loading registered power tools --charge registered power tools
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("src")==false then if inv:is_empty("src")==false then
local srcstack = inv:get_stack("src", 1) local srcstack = inv:get_stack("src", 1)
@ -197,13 +192,10 @@ minetest.register_abm({
src_meta["charge"]=0 src_meta["charge"]=0
end end
end end
item_max_charge=power_tools[i].max_charge -- Do the charging
end local item_max_charge = power_tools[i].max_charge
end local load1 = src_meta["charge"]
local load_step = 1000 -- how much to charge per tick
if item_max_charge then
load1=src_meta["charge"]
load_step=1000
if load1<item_max_charge and charge>0 then if load1<item_max_charge and charge>0 then
if charge-load_step<0 then load_step=charge end if charge-load_step<0 then load_step=charge end
if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end
@ -214,7 +206,9 @@ minetest.register_abm({
src_item["metadata"]=set_item_meta(src_meta) src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("src", 1, src_item) inv:set_stack("src", 1, src_item)
end end
meta:set_int("battery_charge",charge) meta:set_int("internal_EU_buffer",charge)
break
end
end end
end end
@ -237,13 +231,9 @@ minetest.register_abm({
src_meta["charge"]=0 src_meta["charge"]=0
end end
end end
item_max_charge=power_tools[i].max_charge local item_max_charge = power_tools[i].max_charge
end
end
if item_max_charge then
local load1 = src_meta["charge"] local load1 = src_meta["charge"]
load_step=1000 local load_step = 4000 -- how much to discharge per tick
if load1>0 and charge<max_charge then if load1>0 and charge<max_charge then
if charge+load_step>max_charge then load_step=max_charge-charge end if charge+load_step>max_charge then load_step=max_charge-charge end
if load1-load_step<0 then load_step=load1 end if load1-load_step<0 then load_step=load1 end
@ -254,30 +244,32 @@ minetest.register_abm({
src_item["metadata"]=set_item_meta(src_meta) src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item) inv:set_stack("dst", 1, src_item)
end end
meta:set_int("internal_EU_buffer",charge)
break
end
end end
end end
meta:set_int("battery_charge",charge)
local load = math.floor(charge/60000 * 100) local load = math.floor(charge/60000 * 100)
meta:set_string("formspec", meta:set_string("formspec",
battery_box_formspec.. battery_box_formspec..
"image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:"..
(load)..":technic_power_meter_fg.png]") (load)..":technic_power_meter_fg.png]"
)
-- Next index the surrounding network the get the producers and receivers on the power grid
local pos1={} local pos1={}
pos1.y=pos.y-1 pos1.y=pos.y-1
pos1.x=pos.x pos1.x=pos.x
pos1.z=pos.z pos1.z=pos.z
meta1 = minetest.env:get_meta(pos1) meta1 = minetest.env:get_meta(pos1)
if meta1:get_float("cablelike")~=1 then return end if meta1:get_float("cablelike")~=1 then return end
local LV_nodes = {} local LV_nodes = {}
local PR_nodes = {} local PR_nodes = {}
local RE_nodes = {} local RE_nodes = {}
local BA_nodes = {}
LV_nodes[1]={} LV_nodes[1]={}
LV_nodes[1].x=pos1.x LV_nodes[1].x=pos1.x
@ -285,26 +277,21 @@ minetest.register_abm({
LV_nodes[1].z=pos1.z LV_nodes[1].z=pos1.z
LV_nodes[1].visited=false LV_nodes[1].visited=false
local table_index=1
table_index=1
repeat repeat
check_LV_node (PR_nodes,RE_nodes,LV_nodes,table_index) check_LV_node(PR_nodes,RE_nodes,BA_nodes,LV_nodes,table_index)
table_index=table_index+1 table_index=table_index+1
if LV_nodes[table_index]==nil then break end if LV_nodes[table_index]==nil then break end
until false until false
local pos1={} -- Get power from all connected producers
i=1 local pr_pos
repeat for _,pr_pos in ipairs(PR_nodes) do
if PR_nodes[i]==nil then break end -- getting power from all connected producers local meta1 = minetest.env:get_meta(pr_pos)
pos1.x=PR_nodes[i].x
pos1.y=PR_nodes[i].y
pos1.z=PR_nodes[i].z
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer") local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local charge_to_take = 200
if charge<max_charge then if charge<max_charge then
charge_to_take=200
if internal_EU_buffer-charge_to_take<=0 then if internal_EU_buffer-charge_to_take<=0 then
charge_to_take=internal_EU_buffer charge_to_take=internal_EU_buffer
end end
@ -314,22 +301,17 @@ i=1
meta1:set_float("internal_EU_buffer",internal_EU_buffer) meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end end
end end
i=i+1 end
until false
if charge>max_charge then charge=max_charge end if charge>max_charge then charge=max_charge end
i=1 -- Provide power to all connected receivers
repeat local re_pos
if RE_nodes[i]==nil then break end for _,re_pos in ipairs(RE_nodes) do
pos1.x=RE_nodes[i].x -- loading all conected machines buffers local meta1 = minetest.env:get_meta(re_pos)
pos1.y=RE_nodes[i].y
pos1.z=RE_nodes[i].z
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer") local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size") local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
local charge_to_give = math.min(200, charge/table.getn(RE_nodes))
local charge_to_give=200
if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then
charge_to_give=internal_EU_buffer_size-internal_EU_buffer charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end end
@ -338,18 +320,22 @@ i=1
internal_EU_buffer=internal_EU_buffer+charge_to_give internal_EU_buffer=internal_EU_buffer+charge_to_give
meta1:set_float("internal_EU_buffer",internal_EU_buffer) meta1:set_float("internal_EU_buffer",internal_EU_buffer)
charge=charge-charge_to_give; charge=charge-charge_to_give;
end
i=i+1
until false
charge=math.floor(charge) charge=math.floor(charge)
charge_string=tostring(charge) meta:set_string("infotext", "LV Battery box: "..charge.."/"..max_charge);
meta:set_string("infotext", "Battery box: "..charge_string.."/"..max_charge); meta:set_int("internal_EU_buffer",charge)
meta:set_int("battery_charge",charge)
end end
}) })
-- Register as a battery type
-- Battery type machines function as power reservoirs and can both receive and give back power
register_LV_machine("technic:battery_box","BA")
for i=1,8,1 do
register_LV_machine("technic:battery_box"..i,"BA")
end
function add_new_cable_node (LV_nodes,pos1) function add_new_cable_node (LV_nodes,pos1)
if LV_nodes == nil then return true end
local i=1 local i=1
repeat repeat
if LV_nodes[i]==nil then break end if LV_nodes[i]==nil then break end
@ -360,49 +346,48 @@ function add_new_cable_node (LV_nodes,pos1)
LV_nodes[i].x=pos1.x LV_nodes[i].x=pos1.x
LV_nodes[i].y=pos1.y LV_nodes[i].y=pos1.y
LV_nodes[i].z=pos1.z LV_nodes[i].z=pos1.z
LV_nodes[i].visited=false
return true return true
end end
function check_LV_node (PR_nodes,RE_nodes,LV_nodes,i) function check_LV_node(PR_nodes,RE_nodes,BA_nodes,LV_nodes,i)
local pos1={} local pos1={}
pos1.x=LV_nodes[i].x pos1.x=LV_nodes[i].x
pos1.y=LV_nodes[i].y pos1.y=LV_nodes[i].y
pos1.z=LV_nodes[i].z pos1.z=LV_nodes[i].z
LV_nodes[i].visited=true
new_node_added=false
pos1.x=pos1.x+1 pos1.x=pos1.x+1
check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1)
pos1.x=pos1.x-2 pos1.x=pos1.x-2
check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1)
pos1.x=pos1.x+1 pos1.x=pos1.x+1
pos1.y=pos1.y+1 pos1.y=pos1.y+1
check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1)
pos1.y=pos1.y-2 pos1.y=pos1.y-2
check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1)
pos1.y=pos1.y+1 pos1.y=pos1.y+1
pos1.z=pos1.z+1 pos1.z=pos1.z+1
check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1)
pos1.z=pos1.z-2 pos1.z=pos1.z-2
check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
return new_node_added return new_node_added
end end
function check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) function check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1)
meta = minetest.env:get_meta(pos1) local meta = minetest.env:get_meta(pos1)
if meta:get_float("cablelike")==1 then new_node_added=add_new_cable_node(LV_nodes,pos1) end local name = minetest.env:get_node(pos1).name
for i in ipairs(LV_machines) do if meta:get_float("cablelike")==1 then
if minetest.env:get_node(pos1).name == LV_machines[i].machine_name then add_new_cable_node(LV_nodes,pos1)
if LV_machines[i].machine_type == "PR" then elseif LV_machines[name] then
new_node_added=add_new_cable_node(PR_nodes,pos1) --print(name.." is a "..LV_machines[name])
end if LV_machines[name] == "PR" then
if LV_machines[i].machine_type == "RE" then add_new_cable_node(PR_nodes,pos1)
new_node_added=add_new_cable_node(RE_nodes,pos1) elseif LV_machines[name] == "RE" then
end add_new_cable_node(RE_nodes,pos1)
elseif LV_machines[name] == "BA" then
add_new_cable_node(BA_nodes,pos1)
end end
end end
end end

@ -1,12 +1,8 @@
HV_machines = {} -- register MV machines here
technic.HV_machines = {}
registered_HV_machines_count=0 local HV_machines = technic.HV_machines
function register_HV_machine(string1,string2) function register_HV_machine(string1,string2)
registered_HV_machines_count=registered_HV_machines_count+1 technic.HV_machines[string1] = string2
HV_machines[registered_HV_machines_count]={}
HV_machines[registered_HV_machines_count].machine_name=string1
HV_machines[registered_HV_machines_count].machine_type=string2
end end
minetest.register_craft({ minetest.register_craft({
@ -30,28 +26,26 @@ hv_battery_box_formspec =
"label[1,3;Power level]".. "label[1,3;Power level]"..
"list[current_player;main;0,5;8,4;]" "list[current_player;main;0,5;8,4;]"
minetest.register_node("technic:hv_battery_box", { minetest.register_node(
"technic:hv_battery_box", {
description = "HV Battery Box", description = "HV Battery Box",
tiles = {"technic_hv_battery_box_top.png", "technic_hv_battery_box_bottom.png", "technic_hv_battery_box_side0.png", tiles = {"technic_hv_battery_box_top.png", "technic_hv_battery_box_bottom.png", "technic_hv_battery_box_side0.png",
"technic_hv_battery_box_side0.png", "technic_hv_battery_box_side0.png", "technic_hv_battery_box_side0.png"}, "technic_hv_battery_box_side0.png", "technic_hv_battery_box_side0.png", "technic_hv_battery_box_side0.png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
technic_mv_power_machine=1,
last_side_shown=0,
drop="technic:hv_battery_box", drop="technic:hv_battery_box",
on_construct = function(pos) on_construct = function(pos)
if pos==nil then return end
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
meta:set_string("infotext", "HV Battery Box") meta:set_string("infotext", "HV Battery Box")
meta:set_float("technic_hv_power_machine", 1) meta:set_float("technic_hv_power_machine", 1)
meta:set_string("formspec", battery_box_formspec) meta:set_string("formspec", battery_box_formspec)
local inv = meta:get_inventory()
inv:set_size("src", 1) inv:set_size("src", 1)
inv:set_size("dst", 1) inv:set_size("dst", 1)
battery_charge = 0
max_charge = 1500000
last_side_shown=0
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
if pos==nil then return end
local meta = minetest.env:get_meta(pos); local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory() local inv = meta:get_inventory()
if not inv:is_empty("dst") then if not inv:is_empty("dst") then
@ -63,9 +57,9 @@ minetest.register_node("technic:hv_battery_box", {
end, end,
}) })
for i=1,8,1 do for i=1,8,1 do
minetest.register_node("technic:hv_battery_box"..i, { minetest.register_node(
"technic:hv_battery_box"..i, {
description = "HV Battery Box", description = "HV Battery Box",
tiles = {"technic_hv_battery_box_top.png", "technic_hv_battery_box_bottom.png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png", tiles = {"technic_hv_battery_box_top.png", "technic_hv_battery_box_bottom.png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png",
"technic_hv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png"}, "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png"},
@ -73,20 +67,15 @@ minetest.register_node("technic:hv_battery_box"..i, {
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
paramtype="light", paramtype="light",
light_source=9, light_source=9,
technic_power_machine=1,
last_side_shown=0,
drop="technic:hv_battery_box", drop="technic:hv_battery_box",
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
meta:set_string("infotext", "HV Battery box") meta:set_string("infotext", "HV Battery box")
meta:set_float("technic_hv_power_machine", 1) meta:set_float("technic_hv_power_machine", 1)
meta:set_string("formspec", battery_box_formspec) meta:set_string("formspec", battery_box_formspec)
local inv = meta:get_inventory()
inv:set_size("src", 1) inv:set_size("src", 1)
inv:set_size("dst", 1) inv:set_size("dst", 1)
battery_charge = 0
max_charge = 1500000
last_side_shown=0
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
local meta = minetest.env:get_meta(pos); local meta = minetest.env:get_meta(pos);
@ -101,37 +90,34 @@ minetest.register_node("technic:hv_battery_box"..i, {
}) })
end end
minetest.register_abm(
HV_nodes_visited = {} {nodenames = {"technic:hv_battery_box","technic:hv_battery_box1","technic:hv_battery_box2","technic:hv_battery_box3","technic:hv_battery_box4",
minetest.register_abm({
nodenames = {"technic:hv_battery_box","technic:hv_battery_box1","technic:hv_battery_box2","technic:hv_battery_box3","technic:hv_battery_box4",
"technic:hv_battery_box5","technic:hv_battery_box6","technic:hv_battery_box7","technic:hv_battery_box8" "technic:hv_battery_box5","technic:hv_battery_box6","technic:hv_battery_box7","technic:hv_battery_box8"
}, },
interval = 1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
charge= meta:get_int("battery_charge") local max_charge = 1500000
max_charge= 1500000 local charge = meta:get_int("internal_EU_buffer")
-- Select node textures
local i = math.ceil((charge/max_charge)*8) local i = math.ceil((charge/max_charge)*8)
if i > 8 then i = 8 end if i > 8 then i = 8 end
j=meta:get_float("last_side_shown") local j = meta:get_float("last_side_shown")
if i~=j then if i~=j then
if i>0 then hacky_swap_node(pos,"technic:hv_battery_box"..i) if i>0 then hacky_swap_node(pos,"technic:hv_battery_box"..i)
elseif i==0 then hacky_swap_node(pos,"technic:hv_battery_box") end elseif i==0 then hacky_swap_node(pos,"technic:hv_battery_box") end
meta:set_float("last_side_shown",i) meta:set_float("last_side_shown",i)
end end
--loading registered power tools --charge registered power tools
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("src")==false then if inv:is_empty("src")==false then
local srcstack = inv:get_stack("src", 1) local srcstack = inv:get_stack("src", 1)
local src_item=srcstack:to_table() local src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"]) local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"]) src_meta=get_item_meta(src_item["metadata"])
@ -145,13 +131,10 @@ minetest.register_abm({
src_meta["charge"]=0 src_meta["charge"]=0
end end
end end
item_max_charge=power_tools[i].max_charge -- Do the charging
end local item_max_charge = power_tools[i].max_charge
end local load1 = src_meta["charge"]
local load_step = 16000 -- how much to charge per tick
if item_max_charge then
load1=src_meta["charge"]
load_step=16000
if load1<item_max_charge and charge>0 then if load1<item_max_charge and charge>0 then
if charge-load_step<0 then load_step=charge end if charge-load_step<0 then load_step=charge end
if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end
@ -162,11 +145,13 @@ minetest.register_abm({
src_item["metadata"]=set_item_meta(src_meta) src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("src", 1, src_item) inv:set_stack("src", 1, src_item)
end end
meta:set_int("battery_charge",charge) meta:set_int("internal_EU_buffer",charge)
break
end
end end
end end
-- dischargin registered power tools -- discharging registered power tools
if inv:is_empty("dst") == false then if inv:is_empty("dst") == false then
srcstack = inv:get_stack("dst", 1) srcstack = inv:get_stack("dst", 1)
src_item=srcstack:to_table() src_item=srcstack:to_table()
@ -185,13 +170,9 @@ minetest.register_abm({
src_meta["charge"]=0 src_meta["charge"]=0
end end
end end
item_max_charge=power_tools[i].max_charge local item_max_charge = power_tools[i].max_charge
end
end
if item_max_charge then
local load1 = src_meta["charge"] local load1 = src_meta["charge"]
load_step=16000 local load_step = 16000 -- how much to discharge per tick
if load1>0 and charge<max_charge then if load1>0 and charge<max_charge then
if charge+load_step>max_charge then load_step=max_charge-charge end if charge+load_step>max_charge then load_step=max_charge-charge end
if load1-load_step<0 then load_step=load1 end if load1-load_step<0 then load_step=load1 end
@ -202,10 +183,12 @@ minetest.register_abm({
src_item["metadata"]=set_item_meta(src_meta) src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item) inv:set_stack("dst", 1, src_item)
end end
meta:set_int("internal_EU_buffer",charge)
break
end
end end
end end
meta:set_int("battery_charge",charge)
local load = math.floor((charge/1500000) * 100) local load = math.floor((charge/1500000) * 100)
meta:set_string("formspec", meta:set_string("formspec",
@ -214,19 +197,19 @@ minetest.register_abm({
(load)..":technic_power_meter_fg.png]" (load)..":technic_power_meter_fg.png]"
) )
-- Next index the surrounding network the get the producers and receivers on the
local pos1={} local pos1={}
pos1.y=pos.y-1 pos1.y=pos.y-1
pos1.x=pos.x pos1.x=pos.x
pos1.z=pos.z pos1.z=pos.z
meta1 = minetest.env:get_meta(pos1) meta1 = minetest.env:get_meta(pos1)
if meta1:get_float("hv_cablelike")~=1 then return end if meta1:get_float("hv_cablelike")~=1 then return end
local HV_nodes = {} local HV_nodes = {}
local PR_nodes = {} local PR_nodes = {}
local RE_nodes = {} local RE_nodes = {}
local BA_nodes = {}
HV_nodes[1]={} HV_nodes[1]={}
HV_nodes[1].x=pos1.x HV_nodes[1].x=pos1.x
@ -234,25 +217,20 @@ minetest.register_abm({
HV_nodes[1].z=pos1.z HV_nodes[1].z=pos1.z
HV_nodes[1].visited=false HV_nodes[1].visited=false
table_index=1 local table_index=1
repeat repeat
check_HV_node (PR_nodes,RE_nodes,HV_nodes,table_index) check_HV_node (PR_nodes,RE_nodes,BA_nodes,HV_nodes,table_index)
table_index=table_index+1 table_index=table_index+1
if HV_nodes[table_index]==nil then break end if HV_nodes[table_index]==nil then break end
until false until false
-- Get power from all connected producers
local pos1={} local pr_pos
i=1 for _,pr_pos in ipairs(PR_nodes) do
repeat local meta1 = minetest.env:get_meta(pr_pos)
if PR_nodes[i]==nil then break end -- gettin power from all connected producers
pos1.x=PR_nodes[i].x
pos1.y=PR_nodes[i].y
pos1.z=PR_nodes[i].z
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer") local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local charge_to_take = 4000
if charge<max_charge then if charge<max_charge then
charge_to_take=4000
if internal_EU_buffer-charge_to_take<=0 then if internal_EU_buffer-charge_to_take<=0 then
charge_to_take=internal_EU_buffer charge_to_take=internal_EU_buffer
end end
@ -262,22 +240,17 @@ i=1
meta1:set_float("internal_EU_buffer",internal_EU_buffer) meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end end
end end
i=i+1 end
until false
if charge>max_charge then charge=max_charge end if charge>max_charge then charge=max_charge end
i=1 -- Provide power to all connected receivers
repeat local re_pos
if RE_nodes[i]==nil then break end for _,re_pos in ipairs(RE_nodes) do
pos1.x=RE_nodes[i].x -- loading all conected machines buffers local meta1 = minetest.env:get_meta(re_pos)
pos1.y=RE_nodes[i].y
pos1.z=RE_nodes[i].z
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer") local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size") local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
local charge_to_give = math.min(4000, charge/table.getn(RE_nodes))
local charge_to_give=4000
if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then
charge_to_give=internal_EU_buffer_size-internal_EU_buffer charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end end
@ -286,18 +259,22 @@ i=1
internal_EU_buffer=internal_EU_buffer+charge_to_give internal_EU_buffer=internal_EU_buffer+charge_to_give
meta1:set_float("internal_EU_buffer",internal_EU_buffer) meta1:set_float("internal_EU_buffer",internal_EU_buffer)
charge=charge-charge_to_give; charge=charge-charge_to_give;
end
i=i+1
until false
charge=math.floor(charge) charge=math.floor(charge)
charge_string=tostring(charge) meta:set_string("infotext", "MV Battery box: "..charge.."/"..max_charge);
meta:set_string("infotext", "HV Battery Box: \n"..charge_string.."/"..max_charge); meta:set_int("internal_EU_buffer",charge)
meta:set_int("battery_charge",charge)
end end
}) })
-- Register as a battery type
-- Battery type machines function as power reservoirs and can both receive and give back power
register_HV_machine("technic:hv_battery_box","BA")
for i=1,8,1 do
register_HV_machine("technic:hv_battery_box"..i,"BA")
end
function add_new_HVcable_node (HV_nodes,pos1) function add_new_HVcable_node (HV_nodes,pos1)
if HV_nodes == nil then return true end
local i=1 local i=1
repeat repeat
if HV_nodes[i]==nil then break end if HV_nodes[i]==nil then break end
@ -308,50 +285,47 @@ HV_nodes[i]={}
HV_nodes[i].x=pos1.x HV_nodes[i].x=pos1.x
HV_nodes[i].y=pos1.y HV_nodes[i].y=pos1.y
HV_nodes[i].z=pos1.z HV_nodes[i].z=pos1.z
HV_nodes[i].visited=false
return true return true
end end
function check_HV_node (PR_nodes,RE_nodes,HV_nodes,i) function check_HV_node(PR_nodes,RE_nodes,BA_nodes,HV_nodes,i)
local pos1={} local pos1={}
pos1.x=HV_nodes[i].x pos1.x=HV_nodes[i].x
pos1.y=HV_nodes[i].y pos1.y=HV_nodes[i].y
pos1.z=HV_nodes[i].z pos1.z=HV_nodes[i].z
HV_nodes[i].visited=true
new_node_added=false
pos1.x=pos1.x+1 pos1.x=pos1.x+1
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.x=pos1.x-2 pos1.x=pos1.x-2
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.x=pos1.x+1 pos1.x=pos1.x+1
pos1.y=pos1.y+1 pos1.y=pos1.y+1
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.y=pos1.y-2 pos1.y=pos1.y-2
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.y=pos1.y+1 pos1.y=pos1.y+1
pos1.z=pos1.z+1 pos1.z=pos1.z+1
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.z=pos1.z-2 pos1.z=pos1.z-2
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
return new_node_added
end end
function check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) function check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
meta = minetest.env:get_meta(pos1) local meta = minetest.env:get_meta(pos1)
if meta:get_float("hv_cablelike")==1 then new_node_added=add_new_HVcable_node(HV_nodes,pos1) end local name = minetest.env:get_node(pos1).name
for i in ipairs(HV_machines) do if meta:get_float("hv_cablelike")==1 then
if minetest.env:get_node(pos1).name == HV_machines[i].machine_name then add_new_HVcable_node(HV_nodes,pos1)
if HV_machines[i].machine_type == "PR" then elseif HV_machines[name] then
new_node_added=add_new_HVcable_node(PR_nodes,pos1) --print(name.." is a "..HV_machines[name])
end if HV_machines[name] == "PR" then
if HV_machines[i].machine_type == "RE" then add_new_HVcable_node(PR_nodes,pos1)
new_node_added=add_new_HVcable_node(RE_nodes,pos1) elseif HV_machines[name] == "RE" then
add_new_HVcable_node(RE_nodes,pos1)
elseif HV_machines[name] == "BA" then
add_new_HVcable_node(BA_nodes,pos1)
end end
end end
end end
end

@ -1,15 +1,13 @@
MV_machines = {} -- register MV machines here
technic.MV_machines = {}
registered_MV_machines_count=0 --local MV_machines = {}
local MV_machines = technic.MV_machines
function register_MV_machine(string1,string2) function register_MV_machine(string1,string2)
registered_MV_machines_count=registered_MV_machines_count+1 technic.MV_machines[string1] = string2
MV_machines[registered_MV_machines_count]={}
MV_machines[registered_MV_machines_count].machine_name=string1
MV_machines[registered_MV_machines_count].machine_type=string2
end end
minetest.register_craft({ minetest.register_craft(
{
output = 'technic:mv_battery_box 1', output = 'technic:mv_battery_box 1',
recipe = { recipe = {
{'technic:battery_box', 'technic:battery_box', 'technic:battery_box'}, {'technic:battery_box', 'technic:battery_box', 'technic:battery_box'},
@ -30,28 +28,28 @@ mv_battery_box_formspec =
"label[1,3;Power level]".. "label[1,3;Power level]"..
"list[current_player;main;0,5;8,4;]" "list[current_player;main;0,5;8,4;]"
minetest.register_node("technic:mv_battery_box", { minetest.register_node(
"technic:mv_battery_box", {
description = "MV Battery Box", description = "MV Battery Box",
tiles = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png", tiles = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png",
"technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png"}, "technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
technic_mv_power_machine=1,
last_side_shown=0,
drop = "technic:mv_battery_box", drop = "technic:mv_battery_box",
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.env:get_meta(pos) if pos==nil then return end
local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory()
meta:set_string("infotext", "MV Battery box") meta:set_string("infotext", "MV Battery box")
meta:set_float("technic_mv_power_machine", 1) meta:set_float("technic_mv_power_machine", 1)
meta:set_string("formspec", battery_box_formspec) meta:set_string("formspec", battery_box_formspec)
local inv = meta:get_inventory() meta:set_float("internal_EU_buffer", 0)
meta:set_float("internal_EU_buffer_size", 300000)
inv:set_size("src", 1) inv:set_size("src", 1)
inv:set_size("dst", 1) inv:set_size("dst", 1)
battery_charge = 0
max_charge = 300000
last_side_shown=0
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
if pos==nil then return end
local meta = minetest.env:get_meta(pos); local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory() local inv = meta:get_inventory()
if not inv:is_empty("dst") then if not inv:is_empty("dst") then
@ -60,33 +58,33 @@ minetest.register_node("technic:mv_battery_box", {
return false return false
end end
return true return true
end, end
}) })
for i=1,8,1 do for i=1,8,1 do
minetest.register_node("technic:mv_battery_box"..i, { minetest.register_node(
"technic:mv_battery_box"..i, {
description = "MV Battery Box", description = "MV Battery Box",
tiles = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", tiles = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png",
"technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png"}, "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
sounds = default.node_sound_wood_defaults(), sounds = default.node_sound_wood_defaults(),
technic_power_machine=1,
last_side_shown=0,
drop = "technic:mv_battery_box", drop = "technic:mv_battery_box",
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.env:get_meta(pos) if pos==nil then return end
local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory()
meta:set_string("infotext", "MV Battery box") meta:set_string("infotext", "MV Battery box")
meta:set_float("technic_mv_power_machine", 1) meta:set_float("technic_mv_power_machine", 1)
meta:set_string("formspec", battery_box_formspec) meta:set_string("formspec", battery_box_formspec)
local inv = meta:get_inventory() meta:set_float("internal_EU_buffer", 0)
meta:set_float("internal_EU_buffer_size", 300000)
inv:set_size("src", 1) inv:set_size("src", 1)
inv:set_size("dst", 1) inv:set_size("dst", 1)
battery_charge = 0
max_charge = 300000
last_side_shown=0
end, end,
can_dig = function(pos,player) can_dig = function(pos,player)
if pos==nil then return end
local meta = minetest.env:get_meta(pos); local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory() local inv = meta:get_inventory()
if not inv:is_empty("dst") then if not inv:is_empty("dst") then
@ -95,43 +93,42 @@ minetest.register_node("technic:mv_battery_box"..i, {
return false return false
end end
return true return true
end, end
}) })
end end
minetest.register_abm(
MV_nodes_visited = {} {nodenames = {"technic:mv_battery_box","technic:mv_battery_box1","technic:mv_battery_box2","technic:mv_battery_box3","technic:mv_battery_box4",
minetest.register_abm({
nodenames = {"technic:mv_battery_box","technic:mv_battery_box1","technic:mv_battery_box2","technic:mv_battery_box3","technic:mv_battery_box4",
"technic:mv_battery_box5","technic:mv_battery_box6","technic:mv_battery_box7","technic:mv_battery_box8" "technic:mv_battery_box5","technic:mv_battery_box6","technic:mv_battery_box7","technic:mv_battery_box8"
}, },
interval = 1, interval = 1,
chance = 1, chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider) action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
charge= meta:get_int("battery_charge") local max_charge = 300000 -- Set maximum charge for the device here
max_charge= 300000 local charge = meta:get_int("internal_EU_buffer")
-- Select node textures
local i = math.ceil((charge/max_charge)*8) local i = math.ceil((charge/max_charge)*8)
if i > 8 then i = 8 end if i > 8 then i = 8 end
j=meta:get_float("last_side_shown") local j = meta:get_float("last_side_shown")
if i~=j then if i~=j then
if i>0 then hacky_swap_node(pos,"technic:mv_battery_box"..i) if i>0 then hacky_swap_node(pos,"technic:mv_battery_box"..i)
elseif i==0 then hacky_swap_node(pos,"technic:mv_battery_box") end elseif i==0 then hacky_swap_node(pos,"technic:mv_battery_box") end
meta:set_float("last_side_shown",i) meta:set_float("last_side_shown",i)
end end
--loading registered power tools --charge registered power tools
local inv = meta:get_inventory() local inv = meta:get_inventory()
if inv:is_empty("src")==false then if inv:is_empty("src")==false then
local srcstack = inv:get_stack("src", 1) local srcstack = inv:get_stack("src", 1)
local src_item=srcstack:to_table() local src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"]) local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil -- Power tools should really be made into a hash table to avoid linear search. But the list is short so okay...
for i=1,registered_power_tools_count,1 do for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then if power_tools[i].tool_name==src_item["name"] then
-- What is this code doing? Setting tool properties if not set already????
src_meta=get_item_meta(src_item["metadata"]) src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then if src_meta==nil then
src_meta={} src_meta={}
@ -143,13 +140,10 @@ minetest.register_abm({
src_meta["charge"]=0 src_meta["charge"]=0
end end
end end
item_max_charge=power_tools[i].max_charge -- Do the charging
end local item_max_charge = power_tools[i].max_charge
end local load1 = src_meta["charge"]
local load_step = 4000 -- how much to charge per tick
if item_max_charge then
load1=src_meta["charge"]
load_step=4000
if load1<item_max_charge and charge>0 then if load1<item_max_charge and charge>0 then
if charge-load_step<0 then load_step=charge end if charge-load_step<0 then load_step=charge end
if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end
@ -160,11 +154,13 @@ minetest.register_abm({
src_item["metadata"] = set_item_meta(src_meta) src_item["metadata"] = set_item_meta(src_meta)
inv:set_stack("src", 1, src_item) inv:set_stack("src", 1, src_item)
end end
meta:set_int("battery_charge",charge) meta:set_int("internal_EU_buffer",charge)
break
end
end end
end end
-- dischargin registered power tools -- discharging registered power tools
if inv:is_empty("dst") == false then if inv:is_empty("dst") == false then
srcstack = inv:get_stack("dst", 1) srcstack = inv:get_stack("dst", 1)
src_item=srcstack:to_table() src_item=srcstack:to_table()
@ -183,13 +179,9 @@ minetest.register_abm({
src_meta["charge"]=0 src_meta["charge"]=0
end end
end end
item_max_charge=power_tools[i].max_charge local item_max_charge = power_tools[i].max_charge
end
end
if item_max_charge then
local load1 = src_meta["charge"] local load1 = src_meta["charge"]
load_step=4000 local load_step = 4000 -- how much to discharge per tick
if load1>0 and charge<max_charge then if load1>0 and charge<max_charge then
if charge+load_step>max_charge then load_step=max_charge-charge end if charge+load_step>max_charge then load_step=max_charge-charge end
if load1-load_step<0 then load_step=load1 end if load1-load_step<0 then load_step=load1 end
@ -200,10 +192,11 @@ minetest.register_abm({
src_item["metadata"]=set_item_meta(src_meta) src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item) inv:set_stack("dst", 1, src_item)
end end
meta:set_int("internal_EU_buffer",charge)
break
end
end end
end end
meta:set_int("battery_charge",charge)
local load = math.floor((charge/300000) * 100) local load = math.floor((charge/300000) * 100)
meta:set_string("formspec", meta:set_string("formspec",
@ -212,45 +205,39 @@ minetest.register_abm({
(load)..":technic_power_meter_fg.png]" (load)..":technic_power_meter_fg.png]"
) )
-- Next index the surrounding network the get the producers and receivers on the power grid
local pos1 = {} local pos1 = {}
pos1.y = pos.y-1 pos1.y = pos.y-1
pos1.x = pos.x pos1.x = pos.x
pos1.z = pos.z pos1.z = pos.z
meta1 = minetest.env:get_meta(pos1) meta1 = minetest.env:get_meta(pos1)
if meta1:get_float("mv_cablelike")~=1 then return end if meta1:get_float("mv_cablelike")~=1 then return end
local MV_nodes = {} local MV_nodes = {}
local PR_nodes = {} local PR_nodes = {}
local RE_nodes = {} local RE_nodes = {}
local BA_nodes = {}
MV_nodes[1] = {} MV_nodes[1] = {}
MV_nodes[1].x = pos1.x MV_nodes[1].x = pos1.x
MV_nodes[1].y = pos1.y MV_nodes[1].y = pos1.y
MV_nodes[1].z = pos1.z MV_nodes[1].z = pos1.z
MV_nodes[1].visited=false
table_index=1 local table_index=1
repeat repeat
check_MV_node (PR_nodes,RE_nodes,MV_nodes,table_index) check_MV_node(PR_nodes,RE_nodes,BA_nodes,MV_nodes,table_index)
table_index=table_index+1 table_index=table_index+1
if MV_nodes[table_index]==nil then break end if MV_nodes[table_index]==nil then break end
until false until false
-- Get power from all connected producers
local pos1={} local pr_pos
i=1 for _,pr_pos in ipairs(PR_nodes) do
repeat local meta1 = minetest.env:get_meta(pr_pos)
if PR_nodes[i]==nil then break end -- gettin power from all connected producers
pos1.x=PR_nodes[i].x
pos1.y=PR_nodes[i].y
pos1.z=PR_nodes[i].z
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer") local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local charge_to_take = 1000
if charge<max_charge then if charge<max_charge then
charge_to_take=1000
if internal_EU_buffer-charge_to_take<=0 then if internal_EU_buffer-charge_to_take<=0 then
charge_to_take=internal_EU_buffer charge_to_take=internal_EU_buffer
end end
@ -260,22 +247,17 @@ i=1
meta1:set_float("internal_EU_buffer",internal_EU_buffer) meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end end
end end
i=i+1 end
until false
if charge>max_charge then charge=max_charge end if charge>max_charge then charge=max_charge end
i=1 -- Provide power to all connected receivers
repeat local re_pos
if RE_nodes[i]==nil then break end for _,re_pos in ipairs(RE_nodes) do
pos1.x=RE_nodes[i].x -- loading all conected machines buffers local meta1 = minetest.env:get_meta(re_pos)
pos1.y=RE_nodes[i].y
pos1.z=RE_nodes[i].z
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer") local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size") local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
local charge_to_give = math.min(1000, charge/table.getn(RE_nodes))
local charge_to_give=1000
if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then
charge_to_give=internal_EU_buffer_size-internal_EU_buffer charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end end
@ -284,18 +266,22 @@ i=1
internal_EU_buffer=internal_EU_buffer+charge_to_give internal_EU_buffer=internal_EU_buffer+charge_to_give
meta1:set_float("internal_EU_buffer",internal_EU_buffer) meta1:set_float("internal_EU_buffer",internal_EU_buffer)
charge=charge-charge_to_give; charge=charge-charge_to_give;
end
i=i+1
until false
charge=math.floor(charge) charge=math.floor(charge)
charge_string=tostring(charge) meta:set_string("infotext", "MV Battery box: "..charge.."/"..max_charge);
meta:set_string("infotext", "Battery box: "..charge_string.."/"..max_charge); meta:set_int("internal_EU_buffer",charge)
meta:set_int("battery_charge",charge)
end end
}) })
-- Register as a battery type
-- Battery type machines function as power reservoirs and can both receive and give back power
register_MV_machine("technic:mv_battery_box","BA")
for i=1,8,1 do
register_MV_machine("technic:mv_battery_box"..i,"BA")
end
function add_new_MVcable_node (MV_nodes,pos1) function add_new_MVcable_node (MV_nodes,pos1)
if MV_nodes == nil then return true end
local i=1 local i=1
repeat repeat
if MV_nodes[i]==nil then break end if MV_nodes[i]==nil then break end
@ -306,50 +292,47 @@ MV_nodes[i]={}
MV_nodes[i].x=pos1.x MV_nodes[i].x=pos1.x
MV_nodes[i].y=pos1.y MV_nodes[i].y=pos1.y
MV_nodes[i].z=pos1.z MV_nodes[i].z=pos1.z
MV_nodes[i].visited=false
return true return true
end end
function check_MV_node (PR_nodes,RE_nodes,MV_nodes,i) function check_MV_node(PR_nodes,RE_nodes,BA_nodes,MV_nodes,i)
local pos1={} local pos1={}
pos1.x=MV_nodes[i].x pos1.x=MV_nodes[i].x
pos1.y=MV_nodes[i].y pos1.y=MV_nodes[i].y
pos1.z=MV_nodes[i].z pos1.z=MV_nodes[i].z
MV_nodes[i].visited=true
new_node_added=false
pos1.x=pos1.x+1 pos1.x=pos1.x+1
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.x=pos1.x-2 pos1.x=pos1.x-2
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.x=pos1.x+1 pos1.x=pos1.x+1
pos1.y=pos1.y+1 pos1.y=pos1.y+1
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.y=pos1.y-2 pos1.y=pos1.y-2
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.y=pos1.y+1 pos1.y=pos1.y+1
pos1.z=pos1.z+1 pos1.z=pos1.z+1
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.z=pos1.z-2 pos1.z=pos1.z-2
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.z=pos1.z+1 pos1.z=pos1.z+1
return new_node_added
end end
function check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) function check_MV_node_subp (PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
meta = minetest.env:get_meta(pos1) local meta = minetest.env:get_meta(pos1)
if meta:get_float("mv_cablelike")==1 then new_node_added=add_new_MVcable_node(MV_nodes,pos1) end local name = minetest.env:get_node(pos1).name
for i in ipairs(MV_machines) do if meta:get_float("mv_cablelike")==1 then
if minetest.env:get_node(pos1).name == MV_machines[i].machine_name then add_new_MVcable_node(MV_nodes,pos1)
if MV_machines[i].machine_type == "PR" then elseif MV_machines[name] then
new_node_added=add_new_MVcable_node(PR_nodes,pos1) --print(name.." is a "..MV_machines[name])
end if MV_machines[name] == "PR" then
if MV_machines[i].machine_type == "RE" then add_new_MVcable_node(PR_nodes,pos1)
new_node_added=add_new_MVcable_node(RE_nodes,pos1) elseif MV_machines[name] == "RE" then
add_new_MVcable_node(RE_nodes,pos1)
elseif MV_machines[name] == "BA" then
add_new_MVcable_node(BA_nodes,pos1)
end end
end end
end end
end

@ -0,0 +1,225 @@
-- The HV down converter will step down HV EUs to MV EUs
-- If we take the solar panel as calibration then the
-- 1 HVEU = 5 MVEU as we stack 5 MV arrays to get a HV array.
-- The downconverter does of course have a conversion loss.
-- This loses 30% of the power.
-- The converter does not store any energy by itself.
minetest.register_node("technic:down_converter_hv", {
description = "HV Down Converter",
tiles = {"technic_hv_down_converter_top.png", "technic_hv_down_converter_bottom.png", "technic_hv_down_converter_side.png",
"technic_hv_down_converter_side.png", "technic_hv_down_converter_side.png", "technic_hv_down_converter_side.png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(),
drawtype = "nodebox",
paramtype = "light",
is_ground_content = true,
node_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
},
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
},
on_construct = function(pos)
local meta = minetest.env:get_meta(pos)
meta:set_float("technic_hv_power_machine", 1)
meta:set_float("technic_mv_power_machine", 1)
meta:set_float("internal_EU_buffer",0)
meta:set_float("internal_EU_buffer_size",0)
meta:set_string("infotext", "HV Down Converter")
meta:set_float("active", false)
end,
})
minetest.register_craft({
output = 'technic:down_converter_hv 1',
recipe = {
{'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot','technic:stainless_steel_ingot'},
{'technic:hv_transformer', 'technic:hv_cable', 'technic:mv_transformer'},
{'technic:hv_cable', 'technic:rubber', 'technic:mv_cable'},
}
})
minetest.register_abm(
{nodenames = {"technic:down_converter_hv"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
-- HV->MV conversion factor
local hv_mv_factor = 5
-- The maximun charge a single converter can handle. Let's set this to
-- what 5 HV solar arrays can produce - 30% loss (2880*5*0.7)
local max_charge = 10080*hv_mv_factor
local meta = minetest.env:get_meta(pos)
local meta1 = nil
local pos1 = {}
local available_charge = 0 -- counted in MV units
local used_charge = 0 -- counted in MV units
-- Index all HV nodes connected to the network
-- HV cable comes in through the bottom
pos1.y = pos.y-1
pos1.x = pos.x
pos1.z = pos.z
meta1 = minetest.env:get_meta(pos1)
if meta1:get_float("hv_cablelike")~=1 then return end
local HV_nodes = {} -- HV type
local HV_PR_nodes = {} -- HV type
local HV_BA_nodes = {} -- HV type
HV_nodes[1] = {}
HV_nodes[1].x = pos1.x
HV_nodes[1].y = pos1.y
HV_nodes[1].z = pos1.z
local table_index = 1
repeat
check_HV_node(HV_PR_nodes,nil,HV_BA_nodes,HV_nodes,table_index)
table_index = table_index + 1
if HV_nodes[table_index] == nil then break end
until false
--print("HV_nodes: PR="..table.getn(HV_PR_nodes).." BA="..table.getn(HV_BA_nodes))
-- Index all MV nodes connected to the network
-- MV cable comes out of the top
pos1.y = pos.y+1
pos1.x = pos.x
pos1.z = pos.z
meta1 = minetest.env:get_meta(pos1)
if meta1:get_float("mv_cablelike")~=1 then return end
local MV_nodes = {} -- MV type
local MV_RE_nodes = {} -- MV type
local MV_BA_nodes = {} -- MV type
MV_nodes[1] = {}
MV_nodes[1].x = pos1.x
MV_nodes[1].y = pos1.y
MV_nodes[1].z = pos1.z
table_index = 1
repeat
check_MV_node(nil,MV_RE_nodes,MV_BA_nodes,MV_nodes,table_index)
table_index = table_index + 1
if MV_nodes[table_index] == nil then break end
until false
--print("MV_nodes: RE="..table.getn(MV_RE_nodes).." BA="..table.getn(MV_BA_nodes))
-- First get available power from all the attached HV suppliers
-- Get the supplier internal EU buffer and read the EUs from it
-- No update yet!
local pos1
-- FIXME: Until further leave the producers out of it and just let the batteries be the hub
-- for _,pos1 in ipairs(HV_PR_nodes) do
-- meta1 = minetest.env:get_meta(pos1)
-- local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
-- available_charge = available_charge + meta1:get_float("internal_EU_buffer") * hv_mv_factor
-- -- Limit conversion capacity
-- if available_charge > max_charge then
-- available_charge = max_charge
-- break
-- end
-- end
-- --print("Available_charge PR:"..available_charge)
for _,pos1 in ipairs(HV_BA_nodes) do
meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
available_charge = available_charge + meta1:get_float("internal_EU_buffer") * hv_mv_factor
-- Limit conversion capacity
if available_charge > max_charge then
available_charge = max_charge
break
end
end
--print("Available_charge PR+BA:"..available_charge)
-- Calculate total number of receivers:
local MV_receivers = table.getn(MV_RE_nodes)+table.getn(MV_BA_nodes)
-- Next supply power to all connected MV machines
-- Get the power receiver internal EU buffer and give EUs to it
-- Note: for now leave out RE type machines until producers distribute power themselves even without a battery
-- for _,pos1 in ipairs(MV_RE_nodes) do
-- local meta1 = minetest.env:get_meta(pos1)
-- local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
-- local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
-- local charge_to_give = math.min(4000, available_charge/MV_receivers) -- power rating limit on the MV wire
-- -- How much can this unit take?
-- if internal_EU_buffer+charge_to_give > internal_EU_buffer_size then
-- charge_to_give=internal_EU_buffer_size-internal_EU_buffer
-- end
-- -- If we are emptying the supply take the remainder
-- if available_charge<used_charge+charge_to_give then charge_to_give=available_charge-used_charge end
-- -- Update the unit supplied to
-- internal_EU_buffer = internal_EU_buffer + charge_to_give
-- meta1:set_float("internal_EU_buffer",internal_EU_buffer)
-- -- Do the accounting
-- used_charge = used_charge + charge_to_give
-- if available_charge == used_charge then break end -- bail out if supply depleted
-- end
--print("used_charge RE:"..used_charge)
for _,pos1 in ipairs(MV_BA_nodes) do
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
--print("internal_EU_buffer:"..internal_EU_buffer)
--print("internal_EU_buffer_size:"..internal_EU_buffer_size)
local charge_to_give = math.min(math.floor(available_charge/MV_receivers), 4000) -- power rating limit on the MV wire
--print("charge_to_give:"..charge_to_give)
-- How much can this unit take?
if internal_EU_buffer+charge_to_give > internal_EU_buffer_size then
charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end
--print("charge_to_give2:"..charge_to_give)
-- If we are emptying the supply take the remainder
if available_charge<used_charge+charge_to_give then charge_to_give=available_charge-used_charge end
-- Update the unit supplied to
--print("charge_to_give3:"..charge_to_give)
internal_EU_buffer = internal_EU_buffer + charge_to_give
--print("internal_EU_buffer:"..internal_EU_buffer)
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
-- Do the accounting
used_charge = used_charge + charge_to_give
--print("used_charge:"..used_charge)
if available_charge == used_charge then break end -- bail out if supply depleted
end
--print("used_charge RE+BA:"..used_charge)
-- Last update the HV suppliers with the actual demand.
-- Get the supplier internal EU buffer and update the EUs from it
-- Note: So far PR nodes left out and only BA nodes are updated
local HV_BA_size = table.getn(HV_BA_nodes)
for _,pos1 in ipairs(HV_BA_nodes) do
meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local charge_to_take = math.floor(used_charge/HV_BA_size/hv_mv_factor) -- HV units
if internal_EU_buffer-charge_to_take <= 0 then
charge_to_take = internal_EU_buffer
end
if charge_to_take > 0 then
internal_EU_buffer = internal_EU_buffer-charge_to_take
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end
end
if used_charge>0 then
meta:set_string("infotext", "HV Down Converter is active (HV:"..available_charge.."/MV:"..used_charge..")");
meta:set_float("active",1) -- used for setting textures someday maybe
else
meta:set_string("infotext", "HV Down Converter is inactive (HV:"..available_charge.."/MV:"..used_charge..")");
meta:set_float("active",0) -- used for setting textures someday maybe
return
end
end,
})
-- This machine does not store energy it receives energy from the HV side and outputs it on the MV side
register_HV_machine ("technic:down_converter_hv","RE")
register_MV_machine ("technic:down_converter_hv","PR")

@ -0,0 +1,226 @@
-- The MV down converter will step down MV EUs to LV EUs
-- If we take the solar panel as calibration then the
-- 1 MVEU = 5 LVEU as we stack 5 LV arrays to get an MV array.
-- The downconverter does of course have a conversion loss.
-- This loses 30% of the power.
-- The converter does not store any energy by itself.
minetest.register_node(
"technic:down_converter_mv", {
description = "MV Down Converter",
tiles = {"technic_mv_down_converter_top.png", "technic_mv_down_converter_bottom.png", "technic_mv_down_converter_side.png",
"technic_mv_down_converter_side.png", "technic_mv_down_converter_side.png", "technic_mv_down_converter_side.png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(),
drawtype = "nodebox",
paramtype = "light",
is_ground_content = true,
node_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
},
selection_box = {
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
},
on_construct = function(pos)
local meta = minetest.env:get_meta(pos)
meta:set_float("technic_mv_power_machine", 1)
meta:set_float("technic_power_machine", 1)
meta:set_float("internal_EU_buffer",0)
meta:set_float("internal_EU_buffer_size",0)
meta:set_string("infotext", "MV Down Converter")
meta:set_float("active", false)
end,
})
minetest.register_craft({
output = 'technic:down_converter_mv 1',
recipe = {
{'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot','technic:stainless_steel_ingot'},
{'technic:mv_transformer', 'technic:mv_cable', 'technic:lv_transformer'},
{'technic:mv_cable', 'technic:rubber', 'technic:lv_cable'},
}
})
minetest.register_abm(
{nodenames = {"technic:down_converter_mv"},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
-- MV->LV conversion factor
local mv_lv_factor = 5
-- The maximun charge a single converter can handle. Let's set this to
-- what 5 MV solar arrays can produce - 30% loss (720*5*0.7)
local max_charge = 2520*mv_lv_factor
local meta = minetest.env:get_meta(pos)
local meta1 = nil
local pos1 = {}
local available_charge = 0 -- counted in LV units
local used_charge = 0 -- counted in LV units
-- Index all MV nodes connected to the network
-- MV cable comes in through the bottom
pos1.y = pos.y-1
pos1.x = pos.x
pos1.z = pos.z
meta1 = minetest.env:get_meta(pos1)
if meta1:get_float("mv_cablelike")~=1 then return end
local MV_nodes = {} -- MV type
local MV_PR_nodes = {} -- MV type
local MV_BA_nodes = {} -- MV type
MV_nodes[1] = {}
MV_nodes[1].x = pos1.x
MV_nodes[1].y = pos1.y
MV_nodes[1].z = pos1.z
local table_index = 1
repeat
check_MV_node(MV_PR_nodes,nil,MV_BA_nodes,MV_nodes,table_index)
table_index = table_index + 1
if MV_nodes[table_index] == nil then break end
until false
--print("MV_nodes: PR="..table.getn(MV_PR_nodes).." BA="..table.getn(MV_BA_nodes))
-- Index all LV nodes connected to the network
-- LV cable comes out of the top
pos1.y = pos.y+1
pos1.x = pos.x
pos1.z = pos.z
meta1 = minetest.env:get_meta(pos1)
if meta1:get_float("cablelike")~=1 then return end
local LV_nodes = {} -- LV type
local LV_RE_nodes = {} -- LV type
local LV_BA_nodes = {} -- LV type
LV_nodes[1] = {}
LV_nodes[1].x = pos1.x
LV_nodes[1].y = pos1.y
LV_nodes[1].z = pos1.z
table_index = 1
repeat
check_LV_node(nil,LV_RE_nodes,LV_BA_nodes,LV_nodes,table_index)
table_index = table_index + 1
if LV_nodes[table_index] == nil then break end
until false
--print("LV_nodes: RE="..table.getn(LV_RE_nodes).." BA="..table.getn(LV_BA_nodes))
-- First get available power from all the attached MV suppliers
-- Get the supplier internal EU buffer and read the EUs from it
-- No update yet!
local pos1
-- FIXME: Until further leave the producers out of it and just let the batteries be the hub
-- for _,pos1 in ipairs(MV_PR_nodes) do
-- meta1 = minetest.env:get_meta(pos1)
-- local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
-- available_charge = available_charge + meta1:get_float("internal_EU_buffer") * mv_lv_factor
-- -- Limit conversion capacity
-- if available_charge > max_charge then
-- available_charge = max_charge
-- break
-- end
-- end
-- print("Available_charge PR:"..available_charge)
for _,pos1 in ipairs(MV_BA_nodes) do
meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
available_charge = available_charge + meta1:get_float("internal_EU_buffer") * mv_lv_factor
-- Limit conversion capacity
if available_charge > max_charge then
available_charge = max_charge
break
end
end
--print("Available_charge PR+BA:"..available_charge)
-- Calculate total number of receivers:
local LV_receivers = table.getn(LV_RE_nodes)+table.getn(LV_BA_nodes)
-- Next supply power to all connected LV machines
-- Get the power receiver internal EU buffer and give EUs to it
-- Note: for now leave out RE type machines until producers distribute power themselves even without a battery
-- for _,pos1 in ipairs(LV_RE_nodes) do
-- local meta1 = minetest.env:get_meta(pos1)
-- local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
-- local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
-- local charge_to_give = math.min(1000, available_charge/LV_receivers) -- power rating limit on the LV wire
-- -- How much can this unit take?
-- if internal_EU_buffer+charge_to_give > internal_EU_buffer_size then
-- charge_to_give=internal_EU_buffer_size-internal_EU_buffer
-- end
-- -- If we are emptying the supply take the remainder
-- if available_charge<used_charge+charge_to_give then charge_to_give=available_charge-used_charge end
-- -- Update the unit supplied to
-- internal_EU_buffer = internal_EU_buffer + charge_to_give
-- meta1:set_float("internal_EU_buffer",internal_EU_buffer)
-- -- Do the accounting
-- used_charge = used_charge + charge_to_give
-- if available_charge == used_charge then break end -- bail out if supply depleted
-- end
--print("used_charge RE:"..used_charge)
for _,pos1 in ipairs(LV_BA_nodes) do
local meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
--print("internal_EU_buffer:"..internal_EU_buffer)
--print("internal_EU_buffer_size:"..internal_EU_buffer_size)
local charge_to_give = math.min(math.floor(available_charge/LV_receivers), 1000) -- power rating limit on the LV wire
--print("charge_to_give:"..charge_to_give)
-- How much can this unit take?
if internal_EU_buffer+charge_to_give > internal_EU_buffer_size then
charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end
--print("charge_to_give2:"..charge_to_give)
-- If we are emptying the supply take the remainder
if available_charge<used_charge+charge_to_give then charge_to_give=available_charge-used_charge end
-- Update the unit supplied to
--print("charge_to_give3:"..charge_to_give)
internal_EU_buffer = internal_EU_buffer + charge_to_give
--print("internal_EU_buffer:"..internal_EU_buffer)
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
-- Do the accounting
used_charge = used_charge + charge_to_give
--print("used_charge:"..used_charge)
if available_charge == used_charge then break end -- bail out if supply depleted
end
--print("used_charge RE+BA:"..used_charge)
-- Last update the MV suppliers with the actual demand.
-- Get the supplier internal EU buffer and update the EUs from it
-- Note: So far PR nodes left out and only BA nodes are updated
local MV_BA_size = table.getn(MV_BA_nodes)
for _,pos1 in ipairs(MV_BA_nodes) do
meta1 = minetest.env:get_meta(pos1)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local charge_to_take = math.floor(used_charge/MV_BA_size/mv_lv_factor) -- MV units
if internal_EU_buffer-charge_to_take <= 0 then
charge_to_take = internal_EU_buffer
end
if charge_to_take > 0 then
internal_EU_buffer = internal_EU_buffer-charge_to_take
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end
end
if used_charge>0 then
meta:set_string("infotext", "MV Down Converter is active (MV:"..available_charge.."/LV:"..used_charge..")");
meta:set_float("active",1) -- used for setting textures someday maybe
else
meta:set_string("infotext", "MV Down Converter is inactive (MV:"..available_charge.."/LV:"..used_charge..")");
meta:set_float("active",0) -- used for setting textures someday maybe
return
end
end,
})
-- This machine does not store energy it receives energy from the MV side and outputs it on the LV side
register_MV_machine ("technic:down_converter_mv","RE")
register_LV_machine ("technic:down_converter_mv","PR")

@ -36,6 +36,7 @@ dofile(modpath.."/cnc_nodes.lua")
dofile(modpath.."/wires_mv.lua") dofile(modpath.."/wires_mv.lua")
dofile(modpath.."/battery_box_mv.lua") dofile(modpath.."/battery_box_mv.lua")
dofile(modpath.."/solar_array_mv.lua") dofile(modpath.."/solar_array_mv.lua")
dofile(modpath.."/down_converter_mv.lua")
dofile(modpath.."/electric_furnace_mv.lua") dofile(modpath.."/electric_furnace_mv.lua")
dofile(modpath.."/alloy_furnace_mv.lua") dofile(modpath.."/alloy_furnace_mv.lua")
dofile(modpath.."/forcefield.lua") dofile(modpath.."/forcefield.lua")
@ -44,6 +45,7 @@ dofile(modpath.."/forcefield.lua")
dofile(modpath.."/wires_hv.lua") dofile(modpath.."/wires_hv.lua")
dofile(modpath.."/battery_box_hv.lua") dofile(modpath.."/battery_box_hv.lua")
dofile(modpath.."/solar_array_hv.lua") dofile(modpath.."/solar_array_hv.lua")
dofile(modpath.."/down_converter_hv.lua")
--Tools --Tools
if technic.config:getBool("enable_mining_drill") then dofile(modpath.."/mining_drill.lua") end if technic.config:getBool("enable_mining_drill") then dofile(modpath.."/mining_drill.lua") end

@ -83,7 +83,8 @@ minetest.register_abm(
meta:set_float("active",1) meta:set_float("active",1)
internal_EU_buffer=internal_EU_buffer+charge_to_give internal_EU_buffer=internal_EU_buffer+charge_to_give
meta:set_float("internal_EU_buffer",internal_EU_buffer) meta:set_float("internal_EU_buffer",internal_EU_buffer)
-- Idea: How about letting solar panels provide power without battery boxes?
-- This could provide an even distribution to all receivers.
else else
meta:set_string("infotext", "Solar Array is inactive"); meta:set_string("infotext", "Solar Array is inactive");
meta:set_float("active",0) meta:set_float("active",0)

Binary file not shown.

After

Width:  |  Height:  |  Size: 653 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 609 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 653 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 670 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 738 B