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,408 +1,393 @@
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) LV_machines[string1] = string2
registered_LV_machines_count=registered_LV_machines_count+1
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 ={}
registered_power_tools_count=0 registered_power_tools_count=0
function register_power_tool (string1,max_charge) function register_power_tool (string1,max_charge)
registered_power_tools_count=registered_power_tools_count+1 registered_power_tools_count=registered_power_tools_count+1
power_tools[registered_power_tools_count]={} power_tools[registered_power_tools_count]={}
power_tools[registered_power_tools_count].tool_name=string1 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)
register_power_tool ("technic:blue_energy_crystal",500000) register_power_tool ("technic:blue_energy_crystal",500000)
minetest.register_craft({ minetest.register_craft({
output = 'technic:battery 1', output = 'technic:battery 1',
recipe = { recipe = {
{'default:wood', 'moreores:copper_ingot', 'default:wood'}, {'default:wood', 'moreores:copper_ingot', 'default:wood'},
{'default:wood', 'moreores:tin_ingot', 'default:wood'}, {'default:wood', 'moreores:tin_ingot', 'default:wood'},
{'default:wood', 'moreores:copper_ingot', 'default:wood'}, {'default:wood', 'moreores:copper_ingot', 'default:wood'},
} }
}) })
minetest.register_craft({ minetest.register_craft({
output = 'technic:battery_box 1', output = 'technic:battery_box 1',
recipe = { recipe = {
{'technic:battery', 'default:wood', 'technic:battery'}, {'technic:battery', 'default:wood', 'technic:battery'},
{'technic:battery', 'moreores:copper_ingot', 'technic:battery'}, {'technic:battery', 'moreores:copper_ingot', 'technic:battery'},
{'default:steel_ingot', 'default:steel_ingot', 'default:steel_ingot'}, {'default:steel_ingot', 'default:steel_ingot', 'default:steel_ingot'},
} }
}) })
minetest.register_tool("technic:battery", minetest.register_tool("technic:battery",
{description = "RE Battery", {description = "RE Battery",
inventory_image = "technic_battery.png", inventory_image = "technic_battery.png",
tool_capabilities = {load=0,max_drop_level=0, groupcaps={fleshy={times={}, uses=10000, maxlevel=0}}}}) tool_capabilities = {load=0,max_drop_level=0, groupcaps={fleshy={times={}, uses=10000, maxlevel=0}}}})
minetest.register_craftitem("technic:battery_box", { minetest.register_craftitem("technic:battery_box", {
description = "Battery box", description = "Battery box",
stack_max = 99, stack_max = 99,
}) })
battery_box_formspec = battery_box_formspec =
"invsize[8,9;]".. "invsize[8,9;]"..
"image[1,1;1,2;technic_power_meter_bg.png]".. "image[1,1;1,2;technic_power_meter_bg.png]"..
"list[current_name;src;3,1;1,1;]".. "list[current_name;src;3,1;1,1;]"..
"image[4,1;1,1;technic_battery_reload.png]".. "image[4,1;1,1;technic_battery_reload.png]"..
"list[current_name;dst;5,1;1,1;]".. "list[current_name;dst;5,1;1,1;]"..
"label[0,0;Battery box]".. "label[0,0;Battery box]"..
"label[3,0;Charge]".. "label[3,0;Charge]"..
"label[5,0;Discharge]".. "label[5,0;Discharge]"..
"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", {
tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.png", "technic_battery_box_side0.png", description = "LV Battery Box",
"technic_battery_box_side0.png", "technic_battery_box_side0.png", "technic_battery_box_side0.png"}, tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.png", "technic_battery_box_side0.png",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, "technic_battery_box_side0.png", "technic_battery_box_side0.png", "technic_battery_box_side0.png"},
sounds = default.node_sound_wood_defaults(), groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
technic_power_machine=1, sounds = default.node_sound_wood_defaults(),
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)
meta:set_string("infotext", "Battery box") local inv = meta:get_inventory()
meta:set_float("technic_power_machine", 1) meta:set_string("infotext", "Battery box")
meta:set_string("formspec", battery_box_formspec) meta:set_float("technic_power_machine", 1)
local inv = meta:get_inventory() meta:set_string("formspec", battery_box_formspec)
inv:set_size("src", 1) meta:set_float("internal_EU_buffer", 0)
inv:set_size("dst", 1) meta:set_float("internal_EU_buffer_size", 60000)
battery_charge = 0 inv:set_size("src", 1)
max_charge = 60000 inv:set_size("dst", 1)
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
return false return false
elseif not inv:is_empty("src") then elseif not inv:is_empty("src") then
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:battery_box"..i, { minetest.register_node(
description = "Battery box", "technic:battery_box"..i, {
tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.png", "technic_battery_box_side0.png^technic_power_meter"..i..".png", description = "LV Battery Box",
"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"}, tiles = {"technic_battery_box_top.png", "technic_battery_box_bottom.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}, "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"},
sounds = default.node_sound_wood_defaults(), groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
technic_power_machine=1, sounds = default.node_sound_wood_defaults(),
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)
meta:set_string("infotext", "Battery box") local inv = meta:get_inventory()
meta:set_float("technic_power_machine", 1) meta:set_string("infotext", "Battery box")
meta:set_string("formspec", battery_box_formspec) meta:set_float("technic_power_machine", 1)
local inv = meta:get_inventory() meta:set_string("formspec", battery_box_formspec)
inv:set_size("src", 1) meta:set_float("internal_EU_buffer", 0)
inv:set_size("dst", 1) meta:set_float("internal_EU_buffer_size", 60000)
battery_charge = 0 inv:set_size("src", 1)
max_charge = 60000 inv:set_size("dst", 1)
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
return false return false
elseif not inv:is_empty("src") then elseif not inv:is_empty("src") then
return false return false
end end
return true return true
end, end,
}) })
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
temp= temp/65535*max_load temp= temp/65535*max_load
return math.floor(temp + 0.5) return math.floor(temp + 0.5)
end end
function set_RE_item_load (load1,max_load) function set_RE_item_load (load1,max_load)
if load1 == 0 then return 65535 end if load1 == 0 then return 65535 end
local temp=load1/max_load*65535 local temp=load1/max_load*65535
temp=65536-temp temp=65536-temp
return math.floor(temp) return math.floor(temp)
end end
function set_RE_wear (item_stack,load1,max_load) function set_RE_wear (item_stack,load1,max_load)
local temp=65536-math.floor(load1/max_load*65535) local temp=65536-math.floor(load1/max_load*65535)
item_stack["wear"]=tostring(temp) 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")
local i=math.ceil((charge/max_charge)*8)
if i>8 then i=8 end
j=meta:get_float("last_side_shown")
if i~=j then
if i>0 then hacky_swap_node(pos,"technic:battery_box"..i)
elseif i==0 then hacky_swap_node(pos,"technic:battery_box") end
meta:set_float("last_side_shown",i)
end
--loading registered power tools -- Select node textures
local inv = meta:get_inventory() local i=math.ceil((charge/max_charge)*8)
if inv:is_empty("src")==false then if i > 8 then i = 8 end
local srcstack = inv:get_stack("src", 1) local j = meta:get_float("last_side_shown")
local src_item=srcstack:to_table() if i~=j then
local src_meta=get_item_meta(src_item["metadata"]) if i>0 then hacky_swap_node(pos,"technic:battery_box"..i)
elseif i==0 then hacky_swap_node(pos,"technic:battery_box") end
local item_max_charge=nil meta:set_float("last_side_shown",i)
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
item_max_charge=power_tools[i].max_charge
end
end end
if item_max_charge then --charge registered power tools
load1=src_meta["charge"] local inv = meta:get_inventory()
load_step=1000 if inv:is_empty("src")==false then
if load1<item_max_charge and charge>0 then local srcstack = inv:get_stack("src", 1)
if charge-load_step<0 then load_step=charge end local src_item=srcstack:to_table()
if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end local src_meta=get_item_meta(src_item["metadata"])
load1=load1+load_step
charge=charge-load_step local item_max_charge=nil
set_RE_wear(src_item,load1,item_max_charge) for i=1,registered_power_tools_count,1 do
src_meta["charge"]=load1 if power_tools[i].tool_name==src_item["name"] then
src_item["metadata"]=set_item_meta(src_meta) src_meta=get_item_meta(src_item["metadata"])
inv:set_stack("src", 1, src_item) if src_meta==nil then
end src_meta={}
meta:set_int("battery_charge",charge) src_meta["technic_power_tool"]=true
end src_meta["charge"]=0
end else
if src_meta["technic_power_tool"]==nil then
-- discharging registered power tools src_meta["technic_power_tool"]=true
if inv:is_empty("dst") == false then src_meta["charge"]=0
srcstack = inv:get_stack("dst", 1) end
src_item=srcstack:to_table() end
local src_meta=get_item_meta(src_item["metadata"]) -- Do the charging
local item_max_charge=nil local item_max_charge = power_tools[i].max_charge
for i=1,registered_power_tools_count,1 do local load1 = src_meta["charge"]
if power_tools[i].tool_name==src_item["name"] then local load_step = 1000 -- how much to charge per tick
src_meta=get_item_meta(src_item["metadata"]) if load1<item_max_charge and charge>0 then
if src_meta==nil then if charge-load_step<0 then load_step=charge end
src_meta={} if load1+load_step>item_max_charge then load_step=item_max_charge-load1 end
src_meta["technic_power_tool"]=true load1=load1+load_step
src_meta["charge"]=0 charge=charge-load_step
else set_RE_wear(src_item,load1,item_max_charge)
if src_meta["technic_power_tool"]==nil then src_meta["charge"]=load1
src_meta["technic_power_tool"]=true src_item["metadata"]=set_item_meta(src_meta)
src_meta["charge"]=0 inv:set_stack("src", 1, src_item)
end end
end meta:set_int("internal_EU_buffer",charge)
item_max_charge=power_tools[i].max_charge break
end end
end end
if item_max_charge then
local load1=src_meta["charge"]
load_step=1000
if load1>0 and charge<max_charge then
if charge+load_step>max_charge then load_step=max_charge-charge end
if load1-load_step<0 then load_step=load1 end
load1=load1-load_step
charge=charge+load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item)
end
end
end end
meta:set_int("battery_charge",charge) -- discharging registered power tools
if inv:is_empty("dst") == false then
srcstack = inv:get_stack("dst", 1)
src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
local item_max_charge = power_tools[i].max_charge
local load1 = src_meta["charge"]
local load_step = 4000 -- how much to discharge per tick
if load1>0 and charge<max_charge then
if charge+load_step>max_charge then load_step=max_charge-charge end
if load1-load_step<0 then load_step=load1 end
load1=load1-load_step
charge=charge+load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item)
end
meta:set_int("internal_EU_buffer",charge)
break
end
end
end
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]"
)
local pos1={} -- Next index the surrounding network the get the producers and receivers on the power grid
local pos1={}
pos1.y=pos.y-1
pos1.x=pos.x
pos1.z=pos.z
pos1.y=pos.y-1 meta1 = minetest.env:get_meta(pos1)
pos1.x=pos.x if meta1:get_float("cablelike")~=1 then return end
pos1.z=pos.z
local LV_nodes = {}
local PR_nodes = {}
local RE_nodes = {}
local BA_nodes = {}
LV_nodes[1]={}
LV_nodes[1].x=pos1.x
LV_nodes[1].y=pos1.y
LV_nodes[1].z=pos1.z
LV_nodes[1].visited=false
local table_index=1
repeat
check_LV_node(PR_nodes,RE_nodes,BA_nodes,LV_nodes,table_index)
table_index=table_index+1
if LV_nodes[table_index]==nil then break end
until false
meta1 = minetest.env:get_meta(pos1) -- Get power from all connected producers
if meta1:get_float("cablelike")~=1 then return end local pr_pos
for _,pr_pos in ipairs(PR_nodes) do
local meta1 = minetest.env:get_meta(pr_pos)
local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
local charge_to_take = 200
if charge<max_charge then
if internal_EU_buffer-charge_to_take<=0 then
charge_to_take=internal_EU_buffer
end
if charge_to_take>0 then
charge=charge+charge_to_take
internal_EU_buffer=internal_EU_buffer-charge_to_take
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end
end
end
local LV_nodes = {} if charge>max_charge then charge=max_charge end
local PR_nodes = {}
local RE_nodes = {}
LV_nodes[1]={} -- Provide power to all connected receivers
LV_nodes[1].x=pos1.x local re_pos
LV_nodes[1].y=pos1.y for _,re_pos in ipairs(RE_nodes) do
LV_nodes[1].z=pos1.z local meta1 = minetest.env:get_meta(re_pos)
LV_nodes[1].visited=false 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(200, charge/table.getn(RE_nodes))
if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then
charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end
if charge-charge_to_give<0 then charge_to_give=charge end
internal_EU_buffer=internal_EU_buffer+charge_to_give
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
charge=charge-charge_to_give;
end
charge=math.floor(charge)
meta:set_string("infotext", "LV Battery box: "..charge.."/"..max_charge);
meta:set_int("internal_EU_buffer",charge)
end
})
table_index=1 -- Register as a battery type
repeat -- Battery type machines function as power reservoirs and can both receive and give back power
check_LV_node (PR_nodes,RE_nodes,LV_nodes,table_index) register_LV_machine("technic:battery_box","BA")
table_index=table_index+1 for i=1,8,1 do
if LV_nodes[table_index]==nil then break end register_LV_machine("technic:battery_box"..i,"BA")
until false
local pos1={}
i=1
repeat
if PR_nodes[i]==nil then break end -- getting 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")
if charge<max_charge then
charge_to_take=200
if internal_EU_buffer-charge_to_take<=0 then
charge_to_take=internal_EU_buffer
end
if charge_to_take>0 then
charge=charge+charge_to_take
internal_EU_buffer=internal_EU_buffer-charge_to_take
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end
end
i=i+1
until false
if charge>max_charge then charge=max_charge end
i=1
repeat
if RE_nodes[i]==nil then break end
pos1.x=RE_nodes[i].x -- loading all conected machines buffers
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_size=meta1:get_float("internal_EU_buffer_size")
local charge_to_give=200
if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then
charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end
if charge-charge_to_give<0 then charge_to_give=charge end
internal_EU_buffer=internal_EU_buffer+charge_to_give
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
charge=charge-charge_to_give;
i=i+1
until false
charge=math.floor(charge)
charge_string=tostring(charge)
meta:set_string("infotext", "Battery box: "..charge_string.."/"..max_charge);
meta:set_int("battery_charge",charge)
end end
})
function add_new_cable_node (LV_nodes,pos1) function add_new_cable_node (LV_nodes,pos1)
local i=1 if LV_nodes == nil then return true end
repeat local i=1
if LV_nodes[i]==nil then break end repeat
if pos1.x==LV_nodes[i].x and pos1.y==LV_nodes[i].y and pos1.z==LV_nodes[i].z then return false end if LV_nodes[i]==nil then break end
i=i+1 if pos1.x==LV_nodes[i].x and pos1.y==LV_nodes[i].y and pos1.z==LV_nodes[i].z then return false end
until false i=i+1
LV_nodes[i]={} until false
LV_nodes[i].x=pos1.x LV_nodes[i]={}
LV_nodes[i].y=pos1.y LV_nodes[i].x=pos1.x
LV_nodes[i].z=pos1.z LV_nodes[i].y=pos1.y
LV_nodes[i].visited=false LV_nodes[i].z=pos1.z
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)
end elseif LV_machines[name] == "BA" then
end add_new_cable_node(BA_nodes,pos1)
end
end
end end

@ -1,357 +1,331 @@
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) technic.HV_machines[string1] = string2
registered_HV_machines_count=registered_HV_machines_count+1
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({
output = 'technic:hv_battery_box 1', output = 'technic:hv_battery_box 1',
recipe = { recipe = {
{'technic:mv_battery_box', 'technic:mv_battery_box', 'mv_technic:battery_box'}, {'technic:mv_battery_box', 'technic:mv_battery_box', 'mv_technic:battery_box'},
{'technic:mv_battery_box', 'technic:hv_transformer', 'mv_technic:battery_box'}, {'technic:mv_battery_box', 'technic:hv_transformer', 'mv_technic:battery_box'},
{'', 'technic:hv_cable', ''}, {'', 'technic:hv_cable', ''},
} }
}) })
hv_battery_box_formspec = hv_battery_box_formspec =
"invsize[8,9;]".. "invsize[8,9;]"..
"image[1,1;1,2;technic_power_meter_bg.png]".. "image[1,1;1,2;technic_power_meter_bg.png]"..
"list[current_name;src;3,1;1,1;]".. "list[current_name;src;3,1;1,1;]"..
"image[4,1;1,1;technic_battery_reload.png]".. "image[4,1;1,1;technic_battery_reload.png]"..
"list[current_name;dst;5,1;1,1;]".. "list[current_name;dst;5,1;1,1;]"..
"label[0,0;HV Battery Box]".. "label[0,0;HV Battery Box]"..
"label[3,0;Charge]".. "label[3,0;Charge]"..
"label[5,0;Discharge]".. "label[5,0;Discharge]"..
"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", {
description = "HV Battery Box",
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"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
sounds = default.node_sound_wood_defaults(),
technic_mv_power_machine=1,
last_side_shown=0,
drop="technic:hv_battery_box",
on_construct = function(pos)
local meta = minetest.env:get_meta(pos)
meta:set_string("infotext", "HV Battery Box")
meta:set_float("technic_hv_power_machine", 1)
meta:set_string("formspec", battery_box_formspec)
local inv = meta:get_inventory()
inv:set_size("src", 1)
inv:set_size("dst", 1)
battery_charge = 0
max_charge = 1500000
last_side_shown=0
end,
can_dig = function(pos,player)
local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory()
if not inv:is_empty("dst") then
return false
elseif not inv:is_empty("src") then
return false
end
return true
end,
})
minetest.register_node(
"technic: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_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},
sounds = default.node_sound_wood_defaults(),
drop="technic:hv_battery_box",
on_construct = function(pos)
if pos==nil then return end
local meta = minetest.env:get_meta(pos)
local inv = meta:get_inventory()
meta:set_string("infotext", "HV Battery Box")
meta:set_float("technic_hv_power_machine", 1)
meta:set_string("formspec", battery_box_formspec)
inv:set_size("src", 1)
inv:set_size("dst", 1)
end,
can_dig = function(pos,player)
if pos==nil then return end
local meta = minetest.env:get_meta(pos);
local inv = meta:get_inventory()
if not inv:is_empty("dst") then
return false
elseif not inv:is_empty("src") then
return false
end
return true
end,
})
for i=1,8,1 do for i=1,8,1 do
minetest.register_node("technic:hv_battery_box"..i, { minetest.register_node(
description = "HV Battery Box", "technic:hv_battery_box"..i, {
tiles = {"technic_hv_battery_box_top.png", "technic_hv_battery_box_bottom.png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png", description = "HV Battery Box",
"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"}, tiles = {"technic_hv_battery_box_top.png", "technic_hv_battery_box_bottom.png", "technic_hv_battery_box_side0.png^technic_power_meter"..i..".png",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, "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"},
sounds = default.node_sound_wood_defaults(), groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
paramtype="light", sounds = default.node_sound_wood_defaults(),
light_source=9, paramtype="light",
technic_power_machine=1, light_source=9,
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) end,
battery_charge = 0 can_dig = function(pos,player)
max_charge = 1500000 local meta = minetest.env:get_meta(pos);
last_side_shown=0 local inv = meta:get_inventory()
end, if not inv:is_empty("dst") then
can_dig = function(pos,player) return false
local meta = minetest.env:get_meta(pos); elseif not inv:is_empty("src") then
local inv = meta:get_inventory() return false
if not inv:is_empty("dst") then end
return false return true
elseif not inv:is_empty("src") then end,
return false })
end
return true
end,
})
end end
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"
},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.env:get_meta(pos)
local max_charge = 1500000
local charge = meta:get_int("internal_EU_buffer")
HV_nodes_visited = {} -- Select node textures
local i = math.ceil((charge/max_charge)*8)
if i > 8 then i = 8 end
minetest.register_abm({ local j = meta:get_float("last_side_shown")
nodenames = {"technic:hv_battery_box","technic:hv_battery_box1","technic:hv_battery_box2","technic:hv_battery_box3","technic:hv_battery_box4", if i~=j then
"technic:hv_battery_box5","technic:hv_battery_box6","technic:hv_battery_box7","technic:hv_battery_box8" 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
interval = 1, meta:set_float("last_side_shown",i)
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.env:get_meta(pos)
charge= meta:get_int("battery_charge")
max_charge= 1500000
local i=math.ceil((charge/max_charge)*8)
if i>8 then i=8 end
j=meta:get_float("last_side_shown")
if i~=j then
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
meta:set_float("last_side_shown",i)
end
--loading registered power tools
local inv = meta:get_inventory()
if inv:is_empty("src")==false then
local srcstack = inv:get_stack("src", 1)
local src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
item_max_charge=power_tools[i].max_charge
end
end
if item_max_charge then
load1=src_meta["charge"]
load_step=16000
if load1<item_max_charge and charge>0 then
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
load1=load1+load_step
charge=charge-load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("src", 1, src_item)
end
meta:set_int("battery_charge",charge)
end
end
-- dischargin registered power tools
if inv:is_empty("dst") == false then
srcstack = inv:get_stack("dst", 1)
src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
item_max_charge=power_tools[i].max_charge
end
end end
if item_max_charge then --charge registered power tools
local load1=src_meta["charge"] local inv = meta:get_inventory()
load_step=16000 if inv:is_empty("src")==false then
if load1>0 and charge<max_charge then local srcstack = inv:get_stack("src", 1)
if charge+load_step>max_charge then load_step=max_charge-charge end local src_item=srcstack:to_table()
if load1-load_step<0 then load_step=load1 end local src_meta=get_item_meta(src_item["metadata"])
load1=load1-load_step
charge=charge+load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item)
end
end
end
meta:set_int("battery_charge",charge)
local load = math.floor((charge/1500000) * 100) for i=1,registered_power_tools_count,1 do
meta:set_string("formspec", if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
-- Do the charging
local item_max_charge = power_tools[i].max_charge
local load1 = src_meta["charge"]
local load_step = 16000 -- how much to charge per tick
if load1<item_max_charge and charge>0 then
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
load1=load1+load_step
charge=charge-load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("src", 1, src_item)
end
meta:set_int("internal_EU_buffer",charge)
break
end
end
end
-- discharging registered power tools
if inv:is_empty("dst") == false then
srcstack = inv:get_stack("dst", 1)
src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
local item_max_charge = power_tools[i].max_charge
local load1 = src_meta["charge"]
local load_step = 16000 -- how much to discharge per tick
if load1>0 and charge<max_charge then
if charge+load_step>max_charge then load_step=max_charge-charge end
if load1-load_step<0 then load_step=load1 end
load1=load1-load_step
charge=charge+load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item)
end
meta:set_int("internal_EU_buffer",charge)
break
end
end
end
local load = math.floor((charge/1500000) * 100)
meta:set_string("formspec",
hv_battery_box_formspec.. hv_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]"
) )
local pos1={}
pos1.y=pos.y-1 -- Next index the surrounding network the get the producers and receivers on the
pos1.x=pos.x local pos1={}
pos1.z=pos.z pos1.y=pos.y-1
pos1.x=pos.x
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
HV_nodes[1].y=pos1.y HV_nodes[1].y=pos1.y
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
repeat
check_HV_node (PR_nodes,RE_nodes,HV_nodes,table_index)
table_index=table_index+1
if HV_nodes[table_index]==nil then break end
until false
local table_index=1
repeat
check_HV_node (PR_nodes,RE_nodes,BA_nodes,HV_nodes,table_index)
table_index=table_index+1
if HV_nodes[table_index]==nil then break end
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 -- gettin power from all connected producers local meta1 = minetest.env:get_meta(pr_pos)
pos1.x=PR_nodes[i].x local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
pos1.y=PR_nodes[i].y local charge_to_take = 4000
pos1.z=PR_nodes[i].z if charge<max_charge then
local meta1 = minetest.env:get_meta(pos1) if internal_EU_buffer-charge_to_take<=0 then
local internal_EU_buffer=meta1:get_float("internal_EU_buffer") charge_to_take=internal_EU_buffer
if charge<max_charge then end
charge_to_take=4000 if charge_to_take>0 then
if internal_EU_buffer-charge_to_take<=0 then charge=charge+charge_to_take
charge_to_take=internal_EU_buffer internal_EU_buffer=internal_EU_buffer-charge_to_take
end meta1:set_float("internal_EU_buffer",internal_EU_buffer)
if charge_to_take>0 then end
charge=charge+charge_to_take end
internal_EU_buffer=internal_EU_buffer-charge_to_take end
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end
end
i=i+1
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 local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
pos1.z=RE_nodes[i].z local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
local meta1 = minetest.env:get_meta(pos1) local charge_to_give = math.min(4000, charge/table.getn(RE_nodes))
local internal_EU_buffer=meta1:get_float("internal_EU_buffer") if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then
local internal_EU_buffer_size=meta1:get_float("internal_EU_buffer_size") charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end
if charge-charge_to_give<0 then charge_to_give=charge end
local charge_to_give=4000 internal_EU_buffer=internal_EU_buffer+charge_to_give
if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then meta1:set_float("internal_EU_buffer",internal_EU_buffer)
charge_to_give=internal_EU_buffer_size-internal_EU_buffer charge=charge-charge_to_give;
end end
if charge-charge_to_give<0 then charge_to_give=charge end charge=math.floor(charge)
meta:set_string("infotext", "MV Battery box: "..charge.."/"..max_charge);
internal_EU_buffer=internal_EU_buffer+charge_to_give meta:set_int("internal_EU_buffer",charge)
meta1:set_float("internal_EU_buffer",internal_EU_buffer) end
charge=charge-charge_to_give; })
i=i+1
until false
charge=math.floor(charge)
charge_string=tostring(charge)
meta:set_string("infotext", "HV Battery Box: \n"..charge_string.."/"..max_charge);
meta:set_int("battery_charge",charge)
-- 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 end
})
function add_new_HVcable_node (HV_nodes,pos1) function add_new_HVcable_node (HV_nodes,pos1)
local i=1 if HV_nodes == nil then return true end
repeat local i=1
if HV_nodes[i]==nil then break end repeat
if pos1.x==HV_nodes[i].x and pos1.y==HV_nodes[i].y and pos1.z==HV_nodes[i].z then return false end if HV_nodes[i]==nil then break end
i=i+1 if pos1.x==HV_nodes[i].x and pos1.y==HV_nodes[i].y and pos1.z==HV_nodes[i].z then return false end
until false i=i+1
HV_nodes[i]={} until false
HV_nodes[i].x=pos1.x HV_nodes[i]={}
HV_nodes[i].y=pos1.y HV_nodes[i].x=pos1.x
HV_nodes[i].z=pos1.z HV_nodes[i].y=pos1.y
HV_nodes[i].visited=false HV_nodes[i].z=pos1.z
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
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1)
pos1.x=pos1.x-2
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1)
pos1.x=pos1.x+1
pos1.y=pos1.y+1
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1)
pos1.y=pos1.y-2
check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1)
pos1.y=pos1.y+1
pos1.z=pos1.z+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.z=pos1.z-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.z=pos1.z+1 pos1.x=pos1.x+1
return new_node_added
pos1.y=pos1.y+1
check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.y=pos1.y-2
check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.y=pos1.y+1
pos1.z=pos1.z+1
check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.z=pos1.z-2
check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1)
pos1.z=pos1.z+1
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
end add_new_HVcable_node(RE_nodes,pos1)
end elseif HV_machines[name] == "BA" then
add_new_HVcable_node(BA_nodes,pos1)
end
end
end end
end

@ -1,355 +1,338 @@
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', {
recipe = { output = 'technic:mv_battery_box 1',
{'technic:battery_box', 'technic:battery_box', 'technic:battery_box'}, recipe = {
{'technic:battery_box', 'technic:mv_transformer', 'technic:battery_box'}, {'technic:battery_box', 'technic:battery_box', 'technic:battery_box'},
{'', 'technic:mv_cable', ''}, {'technic:battery_box', 'technic:mv_transformer', 'technic:battery_box'},
} {'', 'technic:mv_cable', ''},
}) }
})
mv_battery_box_formspec = mv_battery_box_formspec =
"invsize[8,9;]".. "invsize[8,9;]"..
"image[1,1;1,2;technic_power_meter_bg.png]".. "image[1,1;1,2;technic_power_meter_bg.png]"..
"list[current_name;src;3,1;1,1;]".. "list[current_name;src;3,1;1,1;]"..
"image[4,1;1,1;technic_battery_reload.png]".. "image[4,1;1,1;technic_battery_reload.png]"..
"list[current_name;dst;5,1;1,1;]".. "list[current_name;dst;5,1;1,1;]"..
"label[0,0;MV_Battery box]".. "label[0,0;MV_Battery box]"..
"label[3,0;Charge]".. "label[3,0;Charge]"..
"label[5,0;Discharge]".. "label[5,0;Discharge]"..
"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(
description = "MV Battery Box", "technic:mv_battery_box", {
tiles = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png", description = "MV Battery Box",
"technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.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",
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, "technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png", "technic_mv_battery_box_side0.png"},
sounds = default.node_sound_wood_defaults(), groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2},
technic_mv_power_machine=1, sounds = default.node_sound_wood_defaults(),
last_side_shown=0, drop = "technic:mv_battery_box",
drop="technic:mv_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);
meta:set_string("infotext", "MV Battery box") local inv = meta:get_inventory()
meta:set_float("technic_mv_power_machine", 1) meta:set_string("infotext", "MV Battery box")
meta:set_string("formspec", battery_box_formspec) meta:set_float("technic_mv_power_machine", 1)
local inv = meta:get_inventory() meta:set_string("formspec", battery_box_formspec)
inv:set_size("src", 1) meta:set_float("internal_EU_buffer", 0)
inv:set_size("dst", 1) meta:set_float("internal_EU_buffer_size", 300000)
battery_charge = 0 inv:set_size("src", 1)
max_charge = 300000 inv:set_size("dst", 1)
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
return false return false
elseif not inv:is_empty("src") then elseif not inv:is_empty("src") then
return false return false
end
return true
end end
return true })
end,
})
for i=1,8,1 do for i=1,8,1 do
minetest.register_node("technic:mv_battery_box"..i, { minetest.register_node(
description = "MV Battery Box", "technic:mv_battery_box"..i, {
tiles = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.png", "technic_mv_battery_box_side0.png^technic_power_meter"..i..".png", description = "MV Battery Box",
"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"}, tiles = {"technic_mv_battery_box_top.png", "technic_mv_battery_box_bottom.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}, "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"},
sounds = default.node_sound_wood_defaults(), groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1},
technic_power_machine=1, sounds = default.node_sound_wood_defaults(),
last_side_shown=0, drop = "technic:mv_battery_box",
drop="technic:mv_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);
meta:set_string("infotext", "MV Battery box") local inv = meta:get_inventory()
meta:set_float("technic_mv_power_machine", 1) meta:set_string("infotext", "MV Battery box")
meta:set_string("formspec", battery_box_formspec) meta:set_float("technic_mv_power_machine", 1)
local inv = meta:get_inventory() meta:set_string("formspec", battery_box_formspec)
inv:set_size("src", 1) meta:set_float("internal_EU_buffer", 0)
inv:set_size("dst", 1) meta:set_float("internal_EU_buffer_size", 300000)
battery_charge = 0 inv:set_size("src", 1)
max_charge = 300000 inv:set_size("dst", 1)
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
return false return false
elseif not inv:is_empty("src") then elseif not inv:is_empty("src") then
return false return false
end end
return true return true
end, end
}) })
end end
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"
},
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.env:get_meta(pos)
local max_charge = 300000 -- Set maximum charge for the device here
local charge = meta:get_int("internal_EU_buffer")
MV_nodes_visited = {} -- Select node textures
local i = math.ceil((charge/max_charge)*8)
if i > 8 then i = 8 end
minetest.register_abm({ local j = meta:get_float("last_side_shown")
nodenames = {"technic:mv_battery_box","technic:mv_battery_box1","technic:mv_battery_box2","technic:mv_battery_box3","technic:mv_battery_box4", if i~=j then
"technic:mv_battery_box5","technic:mv_battery_box6","technic:mv_battery_box7","technic:mv_battery_box8" 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
interval = 1, meta:set_float("last_side_shown",i)
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.env:get_meta(pos)
charge= meta:get_int("battery_charge")
max_charge= 300000
local i=math.ceil((charge/max_charge)*8)
if i>8 then i=8 end
j=meta:get_float("last_side_shown")
if i~=j then
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
meta:set_float("last_side_shown",i)
end
--loading registered power tools
local inv = meta:get_inventory()
if inv:is_empty("src")==false then
local srcstack = inv:get_stack("src", 1)
local src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
item_max_charge=power_tools[i].max_charge
end
end
if item_max_charge then
load1=src_meta["charge"]
load_step=4000
if load1<item_max_charge and charge>0 then
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
load1=load1+load_step
charge=charge-load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("src", 1, src_item)
end
meta:set_int("battery_charge",charge)
end
end
-- dischargin registered power tools
if inv:is_empty("dst") == false then
srcstack = inv:get_stack("dst", 1)
src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
item_max_charge=power_tools[i].max_charge
end
end end
if item_max_charge then --charge registered power tools
local load1=src_meta["charge"] local inv = meta:get_inventory()
load_step=4000 if inv:is_empty("src")==false then
if load1>0 and charge<max_charge then local srcstack = inv:get_stack("src", 1)
if charge+load_step>max_charge then load_step=max_charge-charge end local src_item=srcstack:to_table()
if load1-load_step<0 then load_step=load1 end local src_meta=get_item_meta(src_item["metadata"])
load1=load1-load_step
charge=charge+load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item)
end
end
end
meta:set_int("battery_charge",charge)
local load = math.floor((charge/300000) * 100) -- Power tools should really be made into a hash table to avoid linear search. But the list is short so okay...
meta:set_string("formspec", for i=1,registered_power_tools_count,1 do
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"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
-- Do the charging
local item_max_charge = power_tools[i].max_charge
local load1 = src_meta["charge"]
local load_step = 4000 -- how much to charge per tick
if load1<item_max_charge and charge>0 then
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
load1=load1+load_step
charge=charge-load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"] = load1
src_item["metadata"] = set_item_meta(src_meta)
inv:set_stack("src", 1, src_item)
end
meta:set_int("internal_EU_buffer",charge)
break
end
end
end
-- discharging registered power tools
if inv:is_empty("dst") == false then
srcstack = inv:get_stack("dst", 1)
src_item=srcstack:to_table()
local src_meta=get_item_meta(src_item["metadata"])
local item_max_charge=nil
for i=1,registered_power_tools_count,1 do
if power_tools[i].tool_name==src_item["name"] then
src_meta=get_item_meta(src_item["metadata"])
if src_meta==nil then
src_meta={}
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
else
if src_meta["technic_power_tool"]==nil then
src_meta["technic_power_tool"]=true
src_meta["charge"]=0
end
end
local item_max_charge = power_tools[i].max_charge
local load1 = src_meta["charge"]
local load_step = 4000 -- how much to discharge per tick
if load1>0 and charge<max_charge then
if charge+load_step>max_charge then load_step=max_charge-charge end
if load1-load_step<0 then load_step=load1 end
load1=load1-load_step
charge=charge+load_step
set_RE_wear(src_item,load1,item_max_charge)
src_meta["charge"]=load1
src_item["metadata"]=set_item_meta(src_meta)
inv:set_stack("dst", 1, src_item)
end
meta:set_int("internal_EU_buffer",charge)
break
end
end
end
local load = math.floor((charge/300000) * 100)
meta:set_string("formspec",
mv_battery_box_formspec.. mv_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]"
) )
local pos1={}
pos1.y=pos.y-1 -- Next index the surrounding network the get the producers and receivers on the power grid
pos1.x=pos.x local pos1 = {}
pos1.z=pos.z pos1.y = pos.y-1
pos1.x = pos.x
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
repeat
check_MV_node (PR_nodes,RE_nodes,MV_nodes,table_index)
table_index=table_index+1
if MV_nodes[table_index]==nil then break end
until false
local table_index=1
repeat
check_MV_node(PR_nodes,RE_nodes,BA_nodes,MV_nodes,table_index)
table_index=table_index+1
if MV_nodes[table_index]==nil then break end
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 -- gettin power from all connected producers local meta1 = minetest.env:get_meta(pr_pos)
pos1.x=PR_nodes[i].x local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
pos1.y=PR_nodes[i].y local charge_to_take = 1000
pos1.z=PR_nodes[i].z if charge<max_charge then
local meta1 = minetest.env:get_meta(pos1) if internal_EU_buffer-charge_to_take<=0 then
local internal_EU_buffer=meta1:get_float("internal_EU_buffer") charge_to_take=internal_EU_buffer
if charge<max_charge then end
charge_to_take=1000 if charge_to_take>0 then
if internal_EU_buffer-charge_to_take<=0 then charge=charge+charge_to_take
charge_to_take=internal_EU_buffer internal_EU_buffer=internal_EU_buffer-charge_to_take
end meta1:set_float("internal_EU_buffer",internal_EU_buffer)
if charge_to_take>0 then end
charge=charge+charge_to_take end
internal_EU_buffer=internal_EU_buffer-charge_to_take end
meta1:set_float("internal_EU_buffer",internal_EU_buffer)
end
end
i=i+1
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 local internal_EU_buffer = meta1:get_float("internal_EU_buffer")
pos1.z=RE_nodes[i].z local internal_EU_buffer_size = meta1:get_float("internal_EU_buffer_size")
local meta1 = minetest.env:get_meta(pos1) local charge_to_give = math.min(1000, charge/table.getn(RE_nodes))
local internal_EU_buffer=meta1:get_float("internal_EU_buffer") if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then
local internal_EU_buffer_size=meta1:get_float("internal_EU_buffer_size") charge_to_give=internal_EU_buffer_size-internal_EU_buffer
end
if charge-charge_to_give<0 then charge_to_give=charge end
local charge_to_give=1000 internal_EU_buffer=internal_EU_buffer+charge_to_give
if internal_EU_buffer+charge_to_give>internal_EU_buffer_size then meta1:set_float("internal_EU_buffer",internal_EU_buffer)
charge_to_give=internal_EU_buffer_size-internal_EU_buffer charge=charge-charge_to_give;
end end
if charge-charge_to_give<0 then charge_to_give=charge end charge=math.floor(charge)
meta:set_string("infotext", "MV Battery box: "..charge.."/"..max_charge);
internal_EU_buffer=internal_EU_buffer+charge_to_give meta:set_int("internal_EU_buffer",charge)
meta1:set_float("internal_EU_buffer",internal_EU_buffer) end
charge=charge-charge_to_give; })
i=i+1
until false
charge=math.floor(charge)
charge_string=tostring(charge)
meta:set_string("infotext", "Battery box: "..charge_string.."/"..max_charge);
meta:set_int("battery_charge",charge)
-- 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 end
})
function add_new_MVcable_node (MV_nodes,pos1) function add_new_MVcable_node (MV_nodes,pos1)
local i=1 if MV_nodes == nil then return true end
repeat local i=1
if MV_nodes[i]==nil then break end repeat
if pos1.x==MV_nodes[i].x and pos1.y==MV_nodes[i].y and pos1.z==MV_nodes[i].z then return false end if MV_nodes[i]==nil then break end
i=i+1 if pos1.x==MV_nodes[i].x and pos1.y==MV_nodes[i].y and pos1.z==MV_nodes[i].z then return false end
until false i=i+1
MV_nodes[i]={} until false
MV_nodes[i].x=pos1.x MV_nodes[i]={}
MV_nodes[i].y=pos1.y MV_nodes[i].x=pos1.x
MV_nodes[i].z=pos1.z MV_nodes[i].y=pos1.y
MV_nodes[i].visited=false MV_nodes[i].z=pos1.z
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
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1)
pos1.x=pos1.x-2
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1)
pos1.x=pos1.x+1
pos1.y=pos1.y+1
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1)
pos1.y=pos1.y-2
check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1)
pos1.y=pos1.y+1
pos1.z=pos1.z+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.z=pos1.z-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.z=pos1.z+1 pos1.x=pos1.x+1
return new_node_added
pos1.y=pos1.y+1
check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.y=pos1.y-2
check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.y=pos1.y+1
pos1.z=pos1.z+1
check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.z=pos1.z-2
check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1)
pos1.z=pos1.z+1
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
end add_new_MVcable_node(RE_nodes,pos1)
end elseif MV_machines[name] == "BA" then
add_new_MVcable_node(BA_nodes,pos1)
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