From: kpoppel Date: Wed, 12 Jun 2013 21:30:37 +0000 (+0200) Subject: Added the Mv and HV down converters. X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=cfb5bd190c8188d0a0ffd43701864cb1de48bd5d;p=zefram%2Fminetest%2Ftechnic.git 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. --- diff --git a/technic/battery_box.lua b/technic/battery_box.lua index 9c91821..5726053 100644 --- a/technic/battery_box.lua +++ b/technic/battery_box.lua @@ -1,408 +1,393 @@ -LV_machines = {} -registered_LV_machines_count=0 - -function register_LV_machine (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 +-- register LV machines here +local LV_machines = {} +function register_LV_machine(string1,string2) + LV_machines[string1] = string2 end power_tools ={} registered_power_tools_count=0 function register_power_tool (string1,max_charge) -registered_power_tools_count=registered_power_tools_count+1 -power_tools[registered_power_tools_count]={} -power_tools[registered_power_tools_count].tool_name=string1 -power_tools[registered_power_tools_count].max_charge=max_charge + registered_power_tools_count=registered_power_tools_count+1 + power_tools[registered_power_tools_count]={} + power_tools[registered_power_tools_count].tool_name=string1 + power_tools[registered_power_tools_count].max_charge=max_charge end - register_power_tool ("technic:battery",10000) register_power_tool ("technic:red_energy_crystal",100000) register_power_tool ("technic:green_energy_crystal",250000) register_power_tool ("technic:blue_energy_crystal",500000) minetest.register_craft({ - output = 'technic:battery 1', - recipe = { - {'default:wood', 'moreores:copper_ingot', 'default:wood'}, - {'default:wood', 'moreores:tin_ingot', 'default:wood'}, - {'default:wood', 'moreores:copper_ingot', 'default:wood'}, - } -}) + output = 'technic:battery 1', + recipe = { + {'default:wood', 'moreores:copper_ingot', 'default:wood'}, + {'default:wood', 'moreores:tin_ingot', 'default:wood'}, + {'default:wood', 'moreores:copper_ingot', 'default:wood'}, + } + }) minetest.register_craft({ - output = 'technic:battery_box 1', - recipe = { - {'technic:battery', 'default:wood', 'technic:battery'}, - {'technic:battery', 'moreores:copper_ingot', 'technic:battery'}, - {'default:steel_ingot', 'default:steel_ingot', 'default:steel_ingot'}, - } -}) + output = 'technic:battery_box 1', + recipe = { + {'technic:battery', 'default:wood', 'technic:battery'}, + {'technic:battery', 'moreores:copper_ingot', 'technic:battery'}, + {'default:steel_ingot', 'default:steel_ingot', 'default:steel_ingot'}, + } + }) minetest.register_tool("technic:battery", -{description = "RE Battery", -inventory_image = "technic_battery.png", -tool_capabilities = {load=0,max_drop_level=0, groupcaps={fleshy={times={}, uses=10000, maxlevel=0}}}}) + {description = "RE Battery", + inventory_image = "technic_battery.png", + tool_capabilities = {load=0,max_drop_level=0, groupcaps={fleshy={times={}, uses=10000, maxlevel=0}}}}) minetest.register_craftitem("technic:battery_box", { - description = "Battery box", - stack_max = 99, -}) + description = "Battery box", + stack_max = 99, + }) battery_box_formspec = - "invsize[8,9;]".. - "image[1,1;1,2;technic_power_meter_bg.png]".. - "list[current_name;src;3,1;1,1;]".. - "image[4,1;1,1;technic_battery_reload.png]".. - "list[current_name;dst;5,1;1,1;]".. - "label[0,0;Battery box]".. - "label[3,0;Charge]".. - "label[5,0;Discharge]".. - "label[1,3;Power level]".. - "list[current_player;main;0,5;8,4;]" - -minetest.register_node("technic:battery_box", { - description = "Battery box", - 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"}, - groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, - sounds = default.node_sound_wood_defaults(), - technic_power_machine=1, - last_side_shown=0, - drop="technic:battery_box", - on_construct = function(pos) - local meta = minetest.env:get_meta(pos) - meta:set_string("infotext", "Battery box") - meta:set_float("technic_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 = 60000 - 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, -}) + "invsize[8,9;]".. + "image[1,1;1,2;technic_power_meter_bg.png]".. + "list[current_name;src;3,1;1,1;]".. + "image[4,1;1,1;technic_battery_reload.png]".. + "list[current_name;dst;5,1;1,1;]".. + "label[0,0;Battery box]".. + "label[3,0;Charge]".. + "label[5,0;Discharge]".. + "label[1,3;Power level]".. + "list[current_player;main;0,5;8,4;]" + +minetest.register_node( + "technic:battery_box", { + description = "LV Battery Box", + 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"}, + groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, + sounds = default.node_sound_wood_defaults(), + drop="technic: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", "Battery box") + meta:set_float("technic_power_machine", 1) + meta:set_string("formspec", battery_box_formspec) + meta:set_float("internal_EU_buffer", 0) + meta:set_float("internal_EU_buffer_size", 60000) + 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 -minetest.register_node("technic:battery_box"..i, { - description = "Battery box", - 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"}, - groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, - sounds = default.node_sound_wood_defaults(), - technic_power_machine=1, - last_side_shown=0, - drop="technic:battery_box", - on_construct = function(pos) - local meta = minetest.env:get_meta(pos) - meta:set_string("infotext", "Battery box") - meta:set_float("technic_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 = 60000 - 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: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", + "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}, + sounds = default.node_sound_wood_defaults(), + drop="technic: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", "Battery box") + meta:set_float("technic_power_machine", 1) + meta:set_string("formspec", battery_box_formspec) + meta:set_float("internal_EU_buffer", 0) + meta:set_float("internal_EU_buffer_size", 60000) + 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, + }) end -LV_nodes_visited = {} - function get_RE_item_load (load1,max_load) -if load1==0 then load1=65535 end -local temp = 65536-load1 -temp= temp/65535*max_load -return math.floor(temp + 0.5) + if load1==0 then load1=65535 end + local temp = 65536-load1 + temp= temp/65535*max_load + return math.floor(temp + 0.5) end function set_RE_item_load (load1,max_load) -if load1 == 0 then return 65535 end -local temp=load1/max_load*65535 -temp=65536-temp -return math.floor(temp) + if load1 == 0 then return 65535 end + local temp=load1/max_load*65535 + temp=65536-temp + return math.floor(temp) end function set_RE_wear (item_stack,load1,max_load) -local temp=65536-math.floor(load1/max_load*65535) -item_stack["wear"]=tostring(temp) -return item_stack + local temp=65536-math.floor(load1/max_load*65535) + item_stack["wear"]=tostring(temp) + return item_stack end -minetest.register_abm({ - 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"}, - interval = 1, - 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= 60000 - 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 - 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 +minetest.register_abm( + {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"}, + 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 = 60000 -- Set maximum charge for the device here + local charge = meta:get_int("internal_EU_buffer") + + -- Select node textures + local i=math.ceil((charge/max_charge)*8) + if i > 8 then i = 8 end + local 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 - if item_max_charge then - load1=src_meta["charge"] - load_step=1000 - if load10 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 - --- 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 - item_max_charge=power_tools[i].max_charge - end - end - - if item_max_charge then - local load1=src_meta["charge"] - load_step=1000 - if load1>0 and chargemax_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 + --charge 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 + -- Do the charging + local item_max_charge = power_tools[i].max_charge + local load1 = src_meta["charge"] + local load_step = 1000 -- how much to charge per tick + if load10 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 - 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 chargemax_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) - meta:set_string("formspec", + local load = math.floor(charge/60000 * 100) + meta:set_string("formspec", battery_box_formspec.. - "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. - (load)..":technic_power_meter_fg.png]") - - local pos1={} - - 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 = {} - local PR_nodes = {} - local RE_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 - - -table_index=1 - repeat - check_LV_node (PR_nodes,RE_nodes,LV_nodes,table_index) - table_index=table_index+1 - if LV_nodes[table_index]==nil then break end - 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 charge0 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) + "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. + (load)..":technic_power_meter_fg.png]" + ) + + -- 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 + + meta1 = minetest.env:get_meta(pos1) + if meta1:get_float("cablelike")~=1 then return end + + 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 + + + -- Get power from all connected producers + 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 charge0 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 + if charge>max_charge then charge=max_charge end + + -- Provide power to all connected receivers + local re_pos + for _,re_pos in ipairs(RE_nodes) do + local meta1 = minetest.env:get_meta(re_pos) + 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 + }) + +-- 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) - local i=1 - repeat - if LV_nodes[i]==nil then break end - 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 - i=i+1 - until false - LV_nodes[i]={} - LV_nodes[i].x=pos1.x - LV_nodes[i].y=pos1.y - LV_nodes[i].z=pos1.z - LV_nodes[i].visited=false - return true + if LV_nodes == nil then return true end + local i=1 + repeat + if LV_nodes[i]==nil then break end + 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 + i=i+1 + until false + LV_nodes[i]={} + LV_nodes[i].x=pos1.x + LV_nodes[i].y=pos1.y + LV_nodes[i].z=pos1.z + return true end -function check_LV_node (PR_nodes,RE_nodes,LV_nodes,i) - local pos1={} - pos1.x=LV_nodes[i].x - pos1.y=LV_nodes[i].y - pos1.z=LV_nodes[i].z - LV_nodes[i].visited=true - new_node_added=false - - pos1.x=pos1.x+1 - check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) - pos1.x=pos1.x-2 - check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) - pos1.x=pos1.x+1 - - pos1.y=pos1.y+1 - check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) - pos1.y=pos1.y-2 - check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) - pos1.y=pos1.y+1 - - pos1.z=pos1.z+1 - check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) - pos1.z=pos1.z-2 - check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) - pos1.z=pos1.z+1 -return new_node_added +function check_LV_node(PR_nodes,RE_nodes,BA_nodes,LV_nodes,i) + local pos1={} + pos1.x=LV_nodes[i].x + pos1.y=LV_nodes[i].y + pos1.z=LV_nodes[i].z + + pos1.x=pos1.x+1 + check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1) + pos1.x=pos1.x-2 + check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1) + pos1.x=pos1.x+1 + + pos1.y=pos1.y+1 + check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1) + pos1.y=pos1.y-2 + check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1) + pos1.y=pos1.y+1 + + pos1.z=pos1.z+1 + check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1) + pos1.z=pos1.z-2 + check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1) + pos1.z=pos1.z+1 + return new_node_added end -function check_LV_node_subp (PR_nodes,RE_nodes,LV_nodes,pos1) -meta = minetest.env:get_meta(pos1) -if meta:get_float("cablelike")==1 then new_node_added=add_new_cable_node(LV_nodes,pos1) end -for i in ipairs(LV_machines) do - if minetest.env:get_node(pos1).name == LV_machines[i].machine_name then - if LV_machines[i].machine_type == "PR" then - new_node_added=add_new_cable_node(PR_nodes,pos1) - end - if LV_machines[i].machine_type == "RE" then - new_node_added=add_new_cable_node(RE_nodes,pos1) - end - end -end +function check_LV_node_subp (PR_nodes,RE_nodes,BA_nodes,LV_nodes,pos1) + local meta = minetest.env:get_meta(pos1) + local name = minetest.env:get_node(pos1).name + if meta:get_float("cablelike")==1 then + add_new_cable_node(LV_nodes,pos1) + elseif LV_machines[name] then + --print(name.." is a "..LV_machines[name]) + if LV_machines[name] == "PR" then + add_new_cable_node(PR_nodes,pos1) + elseif LV_machines[name] == "RE" then + add_new_cable_node(RE_nodes,pos1) + elseif LV_machines[name] == "BA" then + add_new_cable_node(BA_nodes,pos1) + end + end end diff --git a/technic/battery_box_hv.lua b/technic/battery_box_hv.lua index e565d4c..16aead9 100644 --- a/technic/battery_box_hv.lua +++ b/technic/battery_box_hv.lua @@ -1,357 +1,331 @@ -HV_machines = {} - -registered_HV_machines_count=0 - -function register_HV_machine (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 +-- register MV machines here +technic.HV_machines = {} +local HV_machines = technic.HV_machines +function register_HV_machine(string1,string2) + technic.HV_machines[string1] = string2 end minetest.register_craft({ - output = 'technic:hv_battery_box 1', - recipe = { - {'technic:mv_battery_box', 'technic:mv_battery_box', 'mv_technic:battery_box'}, - {'technic:mv_battery_box', 'technic:hv_transformer', 'mv_technic:battery_box'}, - {'', 'technic:hv_cable', ''}, - } -}) + output = 'technic:hv_battery_box 1', + recipe = { + {'technic:mv_battery_box', 'technic:mv_battery_box', 'mv_technic:battery_box'}, + {'technic:mv_battery_box', 'technic:hv_transformer', 'mv_technic:battery_box'}, + {'', 'technic:hv_cable', ''}, + } + }) hv_battery_box_formspec = - "invsize[8,9;]".. - "image[1,1;1,2;technic_power_meter_bg.png]".. - "list[current_name;src;3,1;1,1;]".. - "image[4,1;1,1;technic_battery_reload.png]".. - "list[current_name;dst;5,1;1,1;]".. - "label[0,0;HV Battery Box]".. - "label[3,0;Charge]".. - "label[5,0;Discharge]".. - "label[1,3;Power level]".. - "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, -}) - + "invsize[8,9;]".. + "image[1,1;1,2;technic_power_meter_bg.png]".. + "list[current_name;src;3,1;1,1;]".. + "image[4,1;1,1;technic_battery_reload.png]".. + "list[current_name;dst;5,1;1,1;]".. + "label[0,0;HV Battery Box]".. + "label[3,0;Charge]".. + "label[5,0;Discharge]".. + "label[1,3;Power level]".. + "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(), + 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 -minetest.register_node("technic:hv_battery_box"..i, { - 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", - "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"}, - groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, - sounds = default.node_sound_wood_defaults(), - paramtype="light", - light_source=9, - technic_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"..i, { + 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", + "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"}, + groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, + sounds = default.node_sound_wood_defaults(), + paramtype="light", + light_source=9, + drop="technic:hv_battery_box", + on_construct = function(pos) + 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) + 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, + }) end - -HV_nodes_visited = {} - - -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) - 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 load10 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 +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") + + -- Select node textures + local i = math.ceil((charge/max_charge)*8) + if i > 8 then i = 8 end + local 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 - if item_max_charge then - local load1=src_meta["charge"] - load_step=16000 - if load1>0 and chargemax_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 + --charge 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"]) + + 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 + -- 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 load10 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 chargemax_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 - - meta:set_int("battery_charge",charge) - local load = math.floor((charge/1500000) * 100) - meta:set_string("formspec", - hv_battery_box_formspec.. - "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. - (load)..":technic_power_meter_fg.png]" - ) - - local pos1={} - pos1.y=pos.y-1 - pos1.x=pos.x - pos1.z=pos.z + local load = math.floor((charge/1500000) * 100) + meta:set_string("formspec", + hv_battery_box_formspec.. + "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. + (load)..":technic_power_meter_fg.png]" + ) + -- Next index the surrounding network the get the producers and receivers on the + local pos1={} + 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 + meta1 = minetest.env:get_meta(pos1) + if meta1:get_float("hv_cablelike")~=1 then return end local HV_nodes = {} local PR_nodes = {} local RE_nodes = {} + local BA_nodes = {} HV_nodes[1]={} HV_nodes[1].x=pos1.x HV_nodes[1].y=pos1.y HV_nodes[1].z=pos1.z 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 pos1={} -i=1 - repeat - 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") - if charge0 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=4000 - 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", "HV Battery Box: \n"..charge_string.."/"..max_charge); - meta:set_int("battery_charge",charge) + 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 + + -- Get power from all connected producers + 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 = 4000 + if charge0 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 + if charge>max_charge then charge=max_charge end + + -- Provide power to all connected receivers + local re_pos + for _,re_pos in ipairs(RE_nodes) do + local meta1 = minetest.env:get_meta(re_pos) + 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, 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", "MV Battery box: "..charge.."/"..max_charge); + meta:set_int("internal_EU_buffer",charge) + 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) -local i=1 - repeat - if HV_nodes[i]==nil then break end - 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 - i=i+1 - until false -HV_nodes[i]={} -HV_nodes[i].x=pos1.x -HV_nodes[i].y=pos1.y -HV_nodes[i].z=pos1.z -HV_nodes[i].visited=false -return true + if HV_nodes == nil then return true end + local i=1 + repeat + if HV_nodes[i]==nil then break end + 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 + i=i+1 + until false + HV_nodes[i]={} + HV_nodes[i].x=pos1.x + HV_nodes[i].y=pos1.y + HV_nodes[i].z=pos1.z + return true end -function check_HV_node (PR_nodes,RE_nodes,HV_nodes,i) - local pos1={} - pos1.x=HV_nodes[i].x - pos1.y=HV_nodes[i].y - 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 - check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) - pos1.z=pos1.z-2 - check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) - pos1.z=pos1.z+1 -return new_node_added +function check_HV_node(PR_nodes,RE_nodes,BA_nodes,HV_nodes,i) + local pos1={} + pos1.x=HV_nodes[i].x + pos1.y=HV_nodes[i].y + pos1.z=HV_nodes[i].z + + pos1.x=pos1.x+1 + check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1) + pos1.x=pos1.x-2 + check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1) + pos1.x=pos1.x+1 + + 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 -function check_HV_node_subp (PR_nodes,RE_nodes,HV_nodes,pos1) -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 -for i in ipairs(HV_machines) do - if minetest.env:get_node(pos1).name == HV_machines[i].machine_name then - if HV_machines[i].machine_type == "PR" then - new_node_added=add_new_HVcable_node(PR_nodes,pos1) - end - if HV_machines[i].machine_type == "RE" then - new_node_added=add_new_HVcable_node(RE_nodes,pos1) - end - end +function check_HV_node_subp (PR_nodes,RE_nodes,BA_nodes,HV_nodes,pos1) + local meta = minetest.env:get_meta(pos1) + local name = minetest.env:get_node(pos1).name + if meta:get_float("hv_cablelike")==1 then + add_new_HVcable_node(HV_nodes,pos1) + elseif HV_machines[name] then + --print(name.." is a "..HV_machines[name]) + if HV_machines[name] == "PR" then + add_new_HVcable_node(PR_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 - diff --git a/technic/battery_box_mv.lua b/technic/battery_box_mv.lua index 8fc6dc7..86c445c 100644 --- a/technic/battery_box_mv.lua +++ b/technic/battery_box_mv.lua @@ -1,355 +1,338 @@ -MV_machines = {} - -registered_MV_machines_count=0 - -function register_MV_machine (string1,string2) -registered_MV_machines_count=registered_MV_machines_count+1 -MV_machines[registered_MV_machines_count]={} -MV_machines[registered_MV_machines_count].machine_name=string1 -MV_machines[registered_MV_machines_count].machine_type=string2 +-- register MV machines here +technic.MV_machines = {} +--local MV_machines = {} +local MV_machines = technic.MV_machines +function register_MV_machine(string1,string2) + technic.MV_machines[string1] = string2 end -minetest.register_craft({ - output = 'technic:mv_battery_box 1', - recipe = { - {'technic:battery_box', 'technic:battery_box', 'technic:battery_box'}, - {'technic:battery_box', 'technic:mv_transformer', 'technic:battery_box'}, - {'', 'technic:mv_cable', ''}, - } -}) +minetest.register_craft( + { + output = 'technic:mv_battery_box 1', + recipe = { + {'technic:battery_box', 'technic:battery_box', 'technic:battery_box'}, + {'technic:battery_box', 'technic:mv_transformer', 'technic:battery_box'}, + {'', 'technic:mv_cable', ''}, + } + }) mv_battery_box_formspec = - "invsize[8,9;]".. - "image[1,1;1,2;technic_power_meter_bg.png]".. - "list[current_name;src;3,1;1,1;]".. - "image[4,1;1,1;technic_battery_reload.png]".. - "list[current_name;dst;5,1;1,1;]".. - "label[0,0;MV_Battery box]".. - "label[3,0;Charge]".. - "label[5,0;Discharge]".. - "label[1,3;Power level]".. - "list[current_player;main;0,5;8,4;]" - -minetest.register_node("technic: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_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}, - sounds = default.node_sound_wood_defaults(), - technic_mv_power_machine=1, - last_side_shown=0, - drop="technic:mv_battery_box", - on_construct = function(pos) - local meta = minetest.env:get_meta(pos) - meta:set_string("infotext", "MV Battery box") - meta:set_float("technic_mv_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 = 300000 - 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 + "invsize[8,9;]".. + "image[1,1;1,2;technic_power_meter_bg.png]".. + "list[current_name;src;3,1;1,1;]".. + "image[4,1;1,1;technic_battery_reload.png]".. + "list[current_name;dst;5,1;1,1;]".. + "label[0,0;MV_Battery box]".. + "label[3,0;Charge]".. + "label[5,0;Discharge]".. + "label[1,3;Power level]".. + "list[current_player;main;0,5;8,4;]" + +minetest.register_node( + "technic: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_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}, + sounds = default.node_sound_wood_defaults(), + drop = "technic:mv_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", "MV Battery box") + meta:set_float("technic_mv_power_machine", 1) + meta:set_string("formspec", battery_box_formspec) + meta:set_float("internal_EU_buffer", 0) + meta:set_float("internal_EU_buffer_size", 300000) + 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 - return true - end, -}) + }) for i=1,8,1 do -minetest.register_node("technic:mv_battery_box"..i, { - 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", - "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}, - sounds = default.node_sound_wood_defaults(), - technic_power_machine=1, - last_side_shown=0, - drop="technic:mv_battery_box", - on_construct = function(pos) - local meta = minetest.env:get_meta(pos) - meta:set_string("infotext", "MV Battery box") - meta:set_float("technic_mv_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 = 300000 - 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:mv_battery_box"..i, { + 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", + "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}, + sounds = default.node_sound_wood_defaults(), + drop = "technic:mv_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", "MV Battery box") + meta:set_float("technic_mv_power_machine", 1) + meta:set_string("formspec", battery_box_formspec) + meta:set_float("internal_EU_buffer", 0) + meta:set_float("internal_EU_buffer_size", 300000) + 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 + }) end - -MV_nodes_visited = {} - - -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) - 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 load10 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 +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") + + -- Select node textures + local i = math.ceil((charge/max_charge)*8) + if i > 8 then i = 8 end + local 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 - if item_max_charge then - local load1=src_meta["charge"] - load_step=4000 - if load1>0 and chargemax_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 + --charge 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"]) + + -- 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 + 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 load10 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 chargemax_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 - - meta:set_int("battery_charge",charge) - local load = math.floor((charge/300000) * 100) - meta:set_string("formspec", + local load = math.floor((charge/300000) * 100) + meta:set_string("formspec", mv_battery_box_formspec.. - "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. - (load)..":technic_power_meter_fg.png]" - ) - - local pos1={} - - pos1.y=pos.y-1 - pos1.x=pos.x - pos1.z=pos.z + "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. + (load)..":technic_power_meter_fg.png]" + ) + -- 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 - meta1 = minetest.env:get_meta(pos1) - if meta1:get_float("mv_cablelike")~=1 then return end + meta1 = minetest.env:get_meta(pos1) + if meta1:get_float("mv_cablelike")~=1 then return end local MV_nodes = {} local PR_nodes = {} local RE_nodes = {} + local BA_nodes = {} + + 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(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 + + -- Get power from all connected producers + 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 = 1000 + if charge0 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 - MV_nodes[1]={} - MV_nodes[1].x=pos1.x - MV_nodes[1].y=pos1.y - 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 pos1={} -i=1 - repeat - 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") - if charge0 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=1000 - 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) - + if charge>max_charge then charge=max_charge end + + -- Provide power to all connected receivers + local re_pos + for _,re_pos in ipairs(RE_nodes) do + local meta1 = minetest.env:get_meta(re_pos) + 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, 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", "MV Battery box: "..charge.."/"..max_charge); + meta:set_int("internal_EU_buffer",charge) + 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) -local i=1 - repeat - if MV_nodes[i]==nil then break end - 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 - i=i+1 - until false -MV_nodes[i]={} -MV_nodes[i].x=pos1.x -MV_nodes[i].y=pos1.y -MV_nodes[i].z=pos1.z -MV_nodes[i].visited=false -return true + if MV_nodes == nil then return true end + local i=1 + repeat + if MV_nodes[i]==nil then break end + 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 + i=i+1 + until false + MV_nodes[i]={} + MV_nodes[i].x=pos1.x + MV_nodes[i].y=pos1.y + MV_nodes[i].z=pos1.z + return true end -function check_MV_node (PR_nodes,RE_nodes,MV_nodes,i) - local pos1={} - pos1.x=MV_nodes[i].x - pos1.y=MV_nodes[i].y - 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 - check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) - pos1.z=pos1.z-2 - check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) - pos1.z=pos1.z+1 -return new_node_added +function check_MV_node(PR_nodes,RE_nodes,BA_nodes,MV_nodes,i) + local pos1={} + pos1.x=MV_nodes[i].x + pos1.y=MV_nodes[i].y + pos1.z=MV_nodes[i].z + + pos1.x=pos1.x+1 + check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1) + pos1.x=pos1.x-2 + check_MV_node_subp(PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1) + pos1.x=pos1.x+1 + + 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 -function check_MV_node_subp (PR_nodes,RE_nodes,MV_nodes,pos1) -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 -for i in ipairs(MV_machines) do - if minetest.env:get_node(pos1).name == MV_machines[i].machine_name then - if MV_machines[i].machine_type == "PR" then - new_node_added=add_new_MVcable_node(PR_nodes,pos1) - end - if MV_machines[i].machine_type == "RE" then - new_node_added=add_new_MVcable_node(RE_nodes,pos1) - end - end -end +function check_MV_node_subp (PR_nodes,RE_nodes,BA_nodes,MV_nodes,pos1) + local meta = minetest.env:get_meta(pos1) + local name = minetest.env:get_node(pos1).name + if meta:get_float("mv_cablelike")==1 then + add_new_MVcable_node(MV_nodes,pos1) + elseif MV_machines[name] then + --print(name.." is a "..MV_machines[name]) + if MV_machines[name] == "PR" then + add_new_MVcable_node(PR_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 - diff --git a/technic/down_converter_hv.lua b/technic/down_converter_hv.lua new file mode 100644 index 0000000..0e2e16d --- /dev/null +++ b/technic/down_converter_hv.lua @@ -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 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 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") diff --git a/technic/down_converter_mv.lua b/technic/down_converter_mv.lua new file mode 100644 index 0000000..1d26630 --- /dev/null +++ b/technic/down_converter_mv.lua @@ -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 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 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") diff --git a/technic/init.lua b/technic/init.lua index 3cc73be..2248295 100644 --- a/technic/init.lua +++ b/technic/init.lua @@ -36,6 +36,7 @@ dofile(modpath.."/cnc_nodes.lua") dofile(modpath.."/wires_mv.lua") dofile(modpath.."/battery_box_mv.lua") dofile(modpath.."/solar_array_mv.lua") +dofile(modpath.."/down_converter_mv.lua") dofile(modpath.."/electric_furnace_mv.lua") dofile(modpath.."/alloy_furnace_mv.lua") dofile(modpath.."/forcefield.lua") @@ -44,6 +45,7 @@ dofile(modpath.."/forcefield.lua") dofile(modpath.."/wires_hv.lua") dofile(modpath.."/battery_box_hv.lua") dofile(modpath.."/solar_array_hv.lua") +dofile(modpath.."/down_converter_hv.lua") --Tools if technic.config:getBool("enable_mining_drill") then dofile(modpath.."/mining_drill.lua") end diff --git a/technic/solar_array_mv.lua b/technic/solar_array_mv.lua index de75301..abcc027 100644 --- a/technic/solar_array_mv.lua +++ b/technic/solar_array_mv.lua @@ -83,7 +83,8 @@ minetest.register_abm( meta:set_float("active",1) internal_EU_buffer=internal_EU_buffer+charge_to_give 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 meta:set_string("infotext", "Solar Array is inactive"); meta:set_float("active",0) diff --git a/technic/textures/technic_hv_down_converter_bottom.png b/technic/textures/technic_hv_down_converter_bottom.png new file mode 100644 index 0000000..996b2d4 Binary files /dev/null and b/technic/textures/technic_hv_down_converter_bottom.png differ diff --git a/technic/textures/technic_hv_down_converter_side.png b/technic/textures/technic_hv_down_converter_side.png new file mode 100644 index 0000000..ab904f1 Binary files /dev/null and b/technic/textures/technic_hv_down_converter_side.png differ diff --git a/technic/textures/technic_hv_down_converter_top.png b/technic/textures/technic_hv_down_converter_top.png new file mode 100644 index 0000000..996b2d4 Binary files /dev/null and b/technic/textures/technic_hv_down_converter_top.png differ diff --git a/technic/textures/technic_mv_down_converter_bottom.png b/technic/textures/technic_mv_down_converter_bottom.png new file mode 100644 index 0000000..a610660 Binary files /dev/null and b/technic/textures/technic_mv_down_converter_bottom.png differ diff --git a/technic/textures/technic_mv_down_converter_side.png b/technic/textures/technic_mv_down_converter_side.png new file mode 100644 index 0000000..6492d5c Binary files /dev/null and b/technic/textures/technic_mv_down_converter_side.png differ diff --git a/technic/textures/technic_mv_down_converter_top.png b/technic/textures/technic_mv_down_converter_top.png new file mode 100644 index 0000000..a610660 Binary files /dev/null and b/technic/textures/technic_mv_down_converter_top.png differ