From 30361afb347ff1a548833e58b1731937584c6e7d Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 12 Jul 2013 15:06:15 -0400 Subject: [PATCH 1/5] Fix forcefield --- technic/machines/hv/forcefield.lua | 210 +++++++++++++++-------------- 1 file changed, 107 insertions(+), 103 deletions(-) diff --git a/technic/machines/hv/forcefield.lua b/technic/machines/hv/forcefield.lua index 8ad5410..3466143 100644 --- a/technic/machines/hv/forcefield.lua +++ b/technic/machines/hv/forcefield.lua @@ -5,7 +5,7 @@ -- Forcefield Generator is a HV machine. -- How expensive is the generator? Leaves room for upgrades lowering the power drain? -local forcefield_power_drain = 10 -- default 10 +local forcefield_power_drain = 10 local forcefield_update_interval = 1 minetest.register_craft({ @@ -17,24 +17,25 @@ minetest.register_craft({ } }) + -- Idea: Let forcefields have different colors by upgrade slot. -- Idea: Let forcefields add up by detecting if one hits another. -- ___ __ -- / \/ \ -- | | -- \___/\___/ --- + + local function add_forcefield(pos, range) for x=-range,range do for y=-range,range do for z=-range,range do - if ((x*x+y*y+z*z) <= (range * range + range)) then - if ((range-1) * (range-1) + (range-1) <= x*x+y*y+z*z) then - local np={x=pos.x+x,y=pos.y+y,z=pos.z+z} - local n = minetest.env:get_node(np).name - if (n == "air") then - minetest.env:add_node(np, {name = "technic:forcefield"}) - end + if (x*x+y*y+z*z) <= (range * range + range) and + (range-1) * (range-1) + (range-1) <= x*x+y*y+z*z then + local node_pos = {x=pos.x+x, y=pos.y+y, z=pos.z+z} + local name = minetest.env:get_node(node_pos).name + if (name == "air") then + minetest.env:add_node(node_pos, {name = "technic:forcefield"}) end end end @@ -43,17 +44,16 @@ local function add_forcefield(pos, range) return true end -local function remove_forcefield(p, range) +local function remove_forcefield(pos, range) for x=-range,range do for y=-range,range do for z=-range,range do - if ((x*x+y*y+z*z) <= (range * range + range)) then - if ((range-1) * (range-1) + (range-1) <= x*x+y*y+z*z) then - local np={x=p.x+x,y=p.y+y,z=p.z+z} - local n = minetest.env:get_node(np).name - if (n == "technic:forcefield") then - minetest.env:remove_node(np) - end + if (x*x+y*y+z*z) <= (range * range + range) and + (range-1) * (range-1) + (range-1) <= x*x+y*y+z*z then + local node_pos = {x=pos.x+x, y=pos.y+y, z=pos.z+z} + local name = minetest.env:get_node(node_pos).name + if (name == "technic:forcefield") then + minetest.env:remove_node(node_pos) end end end @@ -62,98 +62,94 @@ local function remove_forcefield(p, range) end local get_forcefield_formspec = function(range) - -- return "invsize[8,9;]".. (if upgrades added later - colors for instance) return "invsize[3,4;]".. - "label[0,0;Forcefield emitter]".. - "label[1,1;Range]".. - "label[1,2;"..range.."]".. - "button[0,2;1,1;subtract;-]".. - "button[2,2;1,1;add;+]".. - "button[0,3;3,1;toggle;Enable/Disable]" -- .. --- "list[current_player;main;0,5;8,4;]" + "label[0,0;Forcefield emitter]".. + "label[1,1;Range]".. + "label[1,2;"..range.."]".. + "button[0,2;1,1;subtract;-]".. + "button[2,2;1,1;add;+]".. + "button[0,3;3,1;toggle;Enable/Disable]" end local forcefield_receive_fields = function(pos, formname, fields, sender) - local meta = minetest.env:get_meta(pos) - local range = meta:get_int("range") - if fields.add then range = range + 1 end - if fields.subtract then range = range - 1 end - if fields.toggle then - if meta:get_int("enabled") == 1 then - meta:set_int("enabled", 0) - else - meta:set_int("enabled", 1) - end - end - -- Smallest field is 5. Anything less is asking for trouble. - -- Largest is 20. It is a matter of pratical node handling. - if range < 5 then range = 5 end - if range > 20 then range = 20 end + local meta = minetest.env:get_meta(pos) + local range = meta:get_int("range") - if range <= 20 and range >= 5 and meta:get_int("range") ~= range then - remove_forcefield(pos, meta:get_int("range")) - meta:set_int("range", range) - meta:set_string("formspec", get_forcefield_formspec(range)) - end - end + if fields.add then range = range + 1 end + if fields.subtract then range = range - 1 end + if fields.toggle then + if meta:get_int("enabled") == 1 then + meta:set_int("enabled", 0) + else + meta:set_int("enabled", 1) + end + end + + -- Smallest field is 5. Anything less is asking for trouble. + -- Largest is 20. It is a matter of pratical node handling. + if range < 5 then range = 5 end + if range > 20 then range = 20 end + + if range <= 20 and range >= 5 and meta:get_int("range") ~= range then + remove_forcefield(pos, meta:get_int("range")) + meta:set_int("range", range) + meta:set_string("formspec", get_forcefield_formspec(range)) + end +end local forcefield_check = function(pos) - local meta = minetest.env:get_meta(pos) - local node = minetest.env:get_node(pos) - local eu_input = meta:get_int("HV_EU_input") - local eu_demand = meta:get_int("HV_EU_demand") - local enabled = meta:get_int("enabled") - - -- Power off automatically if no longer connected to a switching station - technic.switching_station_timeout_count(pos, "HV") + local meta = minetest.env:get_meta(pos) + local node = minetest.env:get_node(pos) + local eu_input = meta:get_int("HV_EU_input") + local eu_demand = meta:get_int("HV_EU_demand") + local enabled = meta:get_int("enabled") - local power_requirement - if enabled == 1 then - power_requirement = math.floor(4*math.pi*math.pow(meta:get_int("range"), 2)) * forcefield_power_drain - else - power_requirement = eu_demand - end + -- Power off automatically if no longer connected to a switching station + technic.switching_station_timeout_count(pos, "HV") - if eu_input == 0 then - meta:set_string("infotext", "Forcefield Generator Unpowered") - meta:set_int("HV_EU_demand", 100) - meta:set_int("enabled", 0) - if node.name == "technic:forcefield_emitter_on" then - remove_forcefield(pos, meta:get_int("range")) - hacky_swap_node(pos, "technic:forcefield_emitter_off") - end - elseif eu_input == power_requirement then - if meta:get_int("enabled") == 1 then - if node.name == "technic:forcefield_emitter_off" then - hacky_swap_node(pos, "technic:forcefield_emitter_on") - meta:set_string("infotext", "Forcefield Generator Active") - add_forcefield(pos, meta:get_int("range")) - else - -- Range updated. Move the forcefield. - add_forcefield(pos, meta:get_int("range")) - end - else - if node.name == "technic:forcefield_emitter_on" then - remove_forcefield(pos, meta:get_int("range")) - hacky_swap_node(pos, "technic:forcefield_emitter_off") - meta:set_int("HV_EU_demand", 100) - meta:set_string("infotext", "Forcefield Generator Idle") - end - end - else - meta:set_int("HV_EU_demand", power_requirement) - end - return true - end - -local mesecons = {effector = { - action_on = function(pos, node) - minetest.env:get_meta(pos):set_int("enabled", 0) - end, - action_off = function(pos, node) - minetest.env:get_meta(pos):set_int("enabled", 1) + local power_requirement = 0 + if enabled == 1 then + power_requirement = math.floor( + 4 * math.pi * math.pow(meta:get_int("range"), 2) + ) * forcefield_power_drain + else + power_requirement = eu_demand end -}} + + if meta:get_int("enabled") == 0 then + if node.name == "technic:forcefield_emitter_on" then + remove_forcefield(pos, meta:get_int("range")) + hacky_swap_node(pos, "technic:forcefield_emitter_off") + meta:set_int("HV_EU_demand", 100) + meta:set_string("infotext", "Forcefield Generator Disabled") + end + elseif eu_input < power_requirement then + meta:set_string("infotext", "Forcefield Generator Unpowered") + if node.name == "technic:forcefield_emitter_on" then + remove_forcefield(pos, meta:get_int("range")) + hacky_swap_node(pos, "technic:forcefield_emitter_off") + end + elseif eu_input >= power_requirement then + if node.name == "technic:forcefield_emitter_off" then + hacky_swap_node(pos, "technic:forcefield_emitter_on") + meta:set_string("infotext", "Forcefield Generator Active") + end + add_forcefield(pos, meta:get_int("range")) + end + meta:set_int("HV_EU_demand", power_requirement) + return true +end + +local mesecons = { + effector = { + action_on = function(pos, node) + minetest.env:get_meta(pos):set_int("enabled", 0) + end, + action_off = function(pos, node) + minetest.env:get_meta(pos):set_int("enabled", 1) + end + } +} minetest.register_node("technic:forcefield_emitter_off", { description = "Forcefield emitter", @@ -208,9 +204,17 @@ minetest.register_node("technic:forcefield", { sunlight_propagates = true, drop = '', light_source = 8, - tiles = {{name="technic_forcefield_animated.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=2.0}}}, + tiles = {{ + name = "technic_forcefield_animated.png", + animation = { + type = "vertical_frames", + aspect_w=16, + aspect_h=16, + length=2.0, + }, + }}, is_ground_content = true, - groups = {not_in_creative_inventory=1, unbreakable=1}, + groups = { not_in_creative_inventory=1, unbreakable=1 }, paramtype = "light", drawtype = "nodebox", node_box = { --hacky way to get the field blue and not see through the ground @@ -221,5 +225,5 @@ minetest.register_node("technic:forcefield", { }, }) -technic.register_HV_machine("technic:forcefield_emitter_on","RE") -technic.register_HV_machine("technic:forcefield_emitter_off","RE") +technic.register_HV_machine("technic:forcefield_emitter_on", "RE") +technic.register_HV_machine("technic:forcefield_emitter_off", "RE") From 4f35f407cebb32cf126a791b610a0bd379366dd0 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 12 Jul 2013 15:41:59 -0400 Subject: [PATCH 2/5] Add LuaVoxelManip support to forcefield --- technic/machines/hv/forcefield.lua | 77 +++++++++++++++--------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/technic/machines/hv/forcefield.lua b/technic/machines/hv/forcefield.lua index 3466143..e693d80 100644 --- a/technic/machines/hv/forcefield.lua +++ b/technic/machines/hv/forcefield.lua @@ -6,7 +6,7 @@ -- How expensive is the generator? Leaves room for upgrades lowering the power drain? local forcefield_power_drain = 10 -local forcefield_update_interval = 1 +local forcefield_step_interval = 1 minetest.register_craft({ output = 'technic:forcefield_emitter_off', @@ -25,40 +25,39 @@ minetest.register_craft({ -- | | -- \___/\___/ +local function update_forcefield(pos, range, active) + local vm = VoxelManip() + local p1 = {x = pos.x-range, y = pos.y-range, z = pos.z-range} + local p2 = {x = pos.x+range, y = pos.y+range, z = pos.z+range} + local MinEdge, MaxEdge = vm:read_from_map(p1, p2) + local area = VoxelArea:new({MinEdge = MinEdge, MaxEdge = MaxEdge}) + local data = vm:get_data() -local function add_forcefield(pos, range) - for x=-range,range do - for y=-range,range do - for z=-range,range do - if (x*x+y*y+z*z) <= (range * range + range) and - (range-1) * (range-1) + (range-1) <= x*x+y*y+z*z then - local node_pos = {x=pos.x+x, y=pos.y+y, z=pos.z+z} - local name = minetest.env:get_node(node_pos).name - if (name == "air") then - minetest.env:add_node(node_pos, {name = "technic:forcefield"}) + local c_air = minetest.get_content_id("air") + local c_field = minetest.get_content_id("technic:forcefield") + + for z=-range, range do + for y=-range, range do + local vi = area:index(pos.x+(-range), pos.y+y, pos.z+z) + for x=-range, range do + if x*x+y*y+z*z <= range * range + range and + x*x+y*y+z*z >= (range-1) * (range-1) + (range-1) and + ((active and data[vi] == c_air) or ((not active) and data[vi] == c_field)) then + if active then + data[vi] = c_field + else + data[vi] = c_air end end + vi = vi + 1 end end end - return true -end -local function remove_forcefield(pos, range) - for x=-range,range do - for y=-range,range do - for z=-range,range do - if (x*x+y*y+z*z) <= (range * range + range) and - (range-1) * (range-1) + (range-1) <= x*x+y*y+z*z then - local node_pos = {x=pos.x+x, y=pos.y+y, z=pos.z+z} - local name = minetest.env:get_node(node_pos).name - if (name == "technic:forcefield") then - minetest.env:remove_node(node_pos) - end - end - end - end - end + vm:set_data(data) + vm:update_liquids() + vm:write_to_map() + vm:update_map() end local get_forcefield_formspec = function(range) @@ -91,13 +90,13 @@ local forcefield_receive_fields = function(pos, formname, fields, sender) if range > 20 then range = 20 end if range <= 20 and range >= 5 and meta:get_int("range") ~= range then - remove_forcefield(pos, meta:get_int("range")) + update_forcefield(pos, meta:get_int("range"), false) meta:set_int("range", range) meta:set_string("formspec", get_forcefield_formspec(range)) end end -local forcefield_check = function(pos) +local function forcefield_step(pos) local meta = minetest.env:get_meta(pos) local node = minetest.env:get_node(pos) local eu_input = meta:get_int("HV_EU_input") @@ -118,7 +117,7 @@ local forcefield_check = function(pos) if meta:get_int("enabled") == 0 then if node.name == "technic:forcefield_emitter_on" then - remove_forcefield(pos, meta:get_int("range")) + update_forcefield(pos, meta:get_int("range"), false) hacky_swap_node(pos, "technic:forcefield_emitter_off") meta:set_int("HV_EU_demand", 100) meta:set_string("infotext", "Forcefield Generator Disabled") @@ -126,7 +125,7 @@ local forcefield_check = function(pos) elseif eu_input < power_requirement then meta:set_string("infotext", "Forcefield Generator Unpowered") if node.name == "technic:forcefield_emitter_on" then - remove_forcefield(pos, meta:get_int("range")) + update_forcefield(pos, meta:get_int("range"), false) hacky_swap_node(pos, "technic:forcefield_emitter_off") end elseif eu_input >= power_requirement then @@ -134,7 +133,7 @@ local forcefield_check = function(pos) hacky_swap_node(pos, "technic:forcefield_emitter_on") meta:set_string("infotext", "Forcefield Generator Active") end - add_forcefield(pos, meta:get_int("range")) + update_forcefield(pos, meta:get_int("range"), true) end meta:set_int("HV_EU_demand", power_requirement) return true @@ -157,10 +156,10 @@ minetest.register_node("technic:forcefield_emitter_off", { tiles = {"technic_forcefield_emitter_off.png"}, is_ground_content = true, groups = {cracky = 1}, - on_timer = forcefield_check, + on_timer = forcefield_step, on_receive_fields = forcefield_receive_fields, on_construct = function(pos) - minetest.env:get_node_timer(pos):start(forcefield_update_interval) + minetest.env:get_node_timer(pos):start(forcefield_step_interval) local meta = minetest.env:get_meta(pos) meta:set_float("technic_hv_power_machine", 1) meta:set_int("HV_EU_input", 0) @@ -179,10 +178,10 @@ minetest.register_node("technic:forcefield_emitter_on", { is_ground_content = true, groups = {cracky = 1, not_in_creative_inventory=1}, drop='"technic:forcefield_emitter_off" 1', - on_timer = forcefield_check, + on_timer = forcefield_step, on_receive_fields = forcefield_receive_fields, on_construct = function(pos) - minetest.env:get_node_timer(pos):start(forcefield_update_interval) + minetest.env:get_node_timer(pos):start(forcefield_step_interval) local meta = minetest.env:get_meta(pos) -- meta:set_float("technic_hv_power_machine", 1) -- meta:set_float("HV_EU_input", 0) @@ -192,8 +191,8 @@ minetest.register_node("technic:forcefield_emitter_on", { meta:set_string("formspec", get_forcefield_formspec(meta:get_int("range"))) -- meta:set_string("infotext", "Forcefield emitter"); end, - on_dig = function(pos, node, digger) - remove_forcefield(pos, minetest.env:get_meta(pos):get_int("range")) + on_dig = function(pos, node, digger) + update_forcefield(pos, minetest.env:get_meta(pos):get_int("range"), false) return minetest.node_dig(pos, node, digger) end, mesecons = mesecons From 187015e0733e5e0bfa5efba215319c7d9d86f072 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 12 Jul 2013 19:23:29 -0400 Subject: [PATCH 3/5] Fix coding style of nuclear reactor --- technic/machines/hv/nuclear_reactor.lua | 408 ++++++++++++------------ 1 file changed, 205 insertions(+), 203 deletions(-) diff --git a/technic/machines/hv/nuclear_reactor.lua b/technic/machines/hv/nuclear_reactor.lua index da5edf0..54defd6 100644 --- a/technic/machines/hv/nuclear_reactor.lua +++ b/technic/machines/hv/nuclear_reactor.lua @@ -5,239 +5,241 @@ -- -- The nuclear reactor core needs water and a protective shield to work. -- This is checked now and then and if the machine is tampered with... BOOM! + local burn_ticks = 24*60 -- [minutes]. How many minutes does the power plant burn per serving? local power_supply = 10000 -- [HV] EUs -local fuel_type = "technic:enriched_uranium" -- This reactor burns this stuff +local fuel_type = "technic:enriched_uranium" -- The reactor burns this stuff + -- FIXME: recipe must make more sense like a rod recepticle, steam chamber, HV generator? -minetest.register_craft( - {output = 'technic:hv_nuclear_reactor_core', - recipe = { - {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot'}, - {'technic:stainless_steel_ingot', '', 'technic:stainless_steel_ingot'}, - {'technic:stainless_steel_ingot', 'technic:hv_cable', 'technic:stainless_steel_ingot'}, - } - }) +minetest.register_craft({ + output = 'technic:hv_nuclear_reactor_core', + recipe = { + {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot'}, + {'technic:stainless_steel_ingot', '', 'technic:stainless_steel_ingot'}, + {'technic:stainless_steel_ingot', 'technic:hv_cable', 'technic:stainless_steel_ingot'}, + } +}) -minetest.register_craftitem( - "technic:hv_nuclear_reactor_core", - {description = "Uranium Rod Driven HV Reactor", - stack_max = 1, - }) +minetest.register_craftitem("technic:hv_nuclear_reactor_core",{ + description = "Uranium Rod Driven HV Reactor", + stack_max = 1, +}) local generator_formspec = - "invsize[8,9;]".. - -- "image[0,0;5,5;technic_generator_menu.png]".. - "label[0,0;Nuclear Reactor Rod Compartment]".. - "list[current_name;src;2,1;3,2;]".. - "list[current_player;main;0,5;8,4;]" + "invsize[8,9;]".. + --"image[0,0;5,5;technic_generator_menu.png]".. + "label[0,0;Nuclear Reactor Rod Compartment]".. + "list[current_name;src;2,1;3,2;]".. + "list[current_player;main;0,5;8,4;]" -- "Boxy sphere" local nodebox = { - { -0.353, -0.353, -0.353, 0.353, 0.353, 0.353 }, -- Box - { -0.495, -0.064, -0.064, 0.495, 0.064, 0.064 }, -- Circle +-x - { -0.483, -0.128, -0.128, 0.483, 0.128, 0.128 }, - { -0.462, -0.191, -0.191, 0.462, 0.191, 0.191 }, - { -0.433, -0.249, -0.249, 0.433, 0.249, 0.249 }, - { -0.397, -0.303, -0.303, 0.397, 0.303, 0.303 }, - { -0.305, -0.396, -0.305, 0.305, 0.396, 0.305 }, -- Circle +-y - { -0.250, -0.432, -0.250, 0.250, 0.432, 0.250 }, - { -0.191, -0.461, -0.191, 0.191, 0.461, 0.191 }, - { -0.130, -0.482, -0.130, 0.130, 0.482, 0.130 }, - { -0.066, -0.495, -0.066, 0.066, 0.495, 0.066 }, - { -0.064, -0.064, -0.495, 0.064, 0.064, 0.495 }, -- Circle +-z - { -0.128, -0.128, -0.483, 0.128, 0.128, 0.483 }, - { -0.191, -0.191, -0.462, 0.191, 0.191, 0.462 }, - { -0.249, -0.249, -0.433, 0.249, 0.249, 0.433 }, - { -0.303, -0.303, -0.397, 0.303, 0.303, 0.397 }, + { -0.353, -0.353, -0.353, 0.353, 0.353, 0.353 }, -- Box + { -0.495, -0.064, -0.064, 0.495, 0.064, 0.064 }, -- Circle +-x + { -0.483, -0.128, -0.128, 0.483, 0.128, 0.128 }, + { -0.462, -0.191, -0.191, 0.462, 0.191, 0.191 }, + { -0.433, -0.249, -0.249, 0.433, 0.249, 0.249 }, + { -0.397, -0.303, -0.303, 0.397, 0.303, 0.303 }, + { -0.305, -0.396, -0.305, 0.305, 0.396, 0.305 }, -- Circle +-y + { -0.250, -0.432, -0.250, 0.250, 0.432, 0.250 }, + { -0.191, -0.461, -0.191, 0.191, 0.461, 0.191 }, + { -0.130, -0.482, -0.130, 0.130, 0.482, 0.130 }, + { -0.066, -0.495, -0.066, 0.066, 0.495, 0.066 }, + { -0.064, -0.064, -0.495, 0.064, 0.064, 0.495 }, -- Circle +-z + { -0.128, -0.128, -0.483, 0.128, 0.128, 0.483 }, + { -0.191, -0.191, -0.462, 0.191, 0.191, 0.462 }, + { -0.249, -0.249, -0.433, 0.249, 0.249, 0.433 }, + { -0.303, -0.303, -0.397, 0.303, 0.303, 0.397 }, } -minetest.register_node( - "technic:hv_nuclear_reactor_core", - {description = "Nuclear Reactor", - tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", - "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", - "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"}, - -- paramtype2 = "facedir", - groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2}, - legacy_facedir_simple = true, - sounds = default.node_sound_wood_defaults(), - drawtype="nodebox", - paramtype = "light", - node_box = { - type = "fixed", - fixed = nodebox - }, - on_construct = function(pos) - local meta = minetest.env:get_meta(pos) - meta:set_string("infotext", "Nuclear Reactor Core") - meta:set_float("technic_hv_power_machine", 1) - meta:set_int("HV_EU_supply", 0) - meta:set_int("HV_EU_from_fuel", 1) -- Signal to the switching station that this device burns some sort of fuel and needs special handling - meta:set_int("burn_time", 0) - meta:set_string("formspec", generator_formspec) - local inv = meta:get_inventory() - inv:set_size("src", 6) - end, - can_dig = function(pos,player) - local meta = minetest.env:get_meta(pos); - local inv = meta:get_inventory() - if not inv:is_empty("src") then - minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); - return false - else - return true - end - end, - }) +minetest.register_node("technic:hv_nuclear_reactor_core", { + description = "Nuclear Reactor", + tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", + "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", + "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"}, + groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2}, + legacy_facedir_simple = true, + sounds = default.node_sound_wood_defaults(), + drawtype="nodebox", + paramtype = "light", + node_box = { + type = "fixed", + fixed = nodebox + }, + on_construct = function(pos) + local meta = minetest.env:get_meta(pos) + meta:set_string("infotext", "Nuclear Reactor Core") + meta:set_float("technic_hv_power_machine", 1) + meta:set_int("HV_EU_supply", 0) + meta:set_int("HV_EU_from_fuel", 1) -- Signal to the switching station that this device burns some sort of fuel and needs special handling + meta:set_int("burn_time", 0) + meta:set_string("formspec", generator_formspec) + local inv = meta:get_inventory() + inv:set_size("src", 6) + end, + can_dig = function(pos,player) + local meta = minetest.env:get_meta(pos); + local inv = meta:get_inventory() + if not inv:is_empty("src") then + minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); + return false + else + return true + end + end, +}) -minetest.register_node( - "technic:hv_nuclear_reactor_core_active", - {description = "Uranium Rod Driven HV Reactor", - tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", - "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"}, - -- paramtype2 = "facedir", - groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, - legacy_facedir_simple = true, - sounds = default.node_sound_wood_defaults(), - drop="technic:hv_nuclear_reactor_core", - drawtype="nodebox", - light_source = 15, - paramtype = "light", - node_box = { - type = "fixed", - fixed = nodebox - }, - can_dig = function(pos,player) - local meta = minetest.env:get_meta(pos); - local inv = meta:get_inventory() - if not inv:is_empty("src") then - minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); - return false - else - return true - end - end, - }) +minetest.register_node("technic:hv_nuclear_reactor_core_active", { + description = "Uranium Rod Driven HV Reactor", + tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", + "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png", + "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"}, + groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, not_in_creative_inventory=1}, + legacy_facedir_simple = true, + sounds = default.node_sound_wood_defaults(), + drop="technic:hv_nuclear_reactor_core", + drawtype="nodebox", + light_source = 15, + paramtype = "light", + node_box = { + type = "fixed", + fixed = nodebox + }, + can_dig = function(pos,player) + local meta = minetest.env:get_meta(pos); + local inv = meta:get_inventory() + if not inv:is_empty("src") then + minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); + return false + else + return true + end + end, +}) local check_reactor_structure = function(pos) - -- The reactor consists of an 11x11x11 cube structure - -- A cross section through the middle: - -- CCCCC CCCCC - -- CCCCC CCCCC - -- CCSSS SSSCC - -- CCSCC CCSCC - -- CCSCWWWCSCC - -- CCSCW#WCSCC - -- CCSCW|WCSCC - -- CCSCC|CCSCC - -- CCSSS|SSSCC - -- CCCCC|CCCCC - -- C = Concrete, S = Stainless Steel, W = water node (not floating), #=reactor core, |=HV cable - -- The man-hole and the HV cable is only in the middle. - local water_nodes = minetest.find_nodes_in_area({x=pos.x-1, y=pos.y-1, z=pos.z-1}, - {x=pos.x+1, y=pos.y+1, z=pos.z+1}, - "default:water_source") - --print("Water ( 25):"..#water_nodes) - if #water_nodes ~= 25 then - --print("Water supply defect") - return 0 - end - local inner_shield_nodes = minetest.find_nodes_in_area({x=pos.x-2, y=pos.y-2, z=pos.z-2}, - {x=pos.x+2, y=pos.y+2, z=pos.z+2}, - "technic:concrete") + -- The reactor consists of an 11x11x11 cube structure + -- A cross section through the middle: + -- CCCCC CCCCC + -- CCCCC CCCCC + -- CCSSS SSSCC + -- CCSCC CCSCC + -- CCSCWWWCSCC + -- CCSCW#WCSCC + -- CCSCW|WCSCC + -- CCSCC|CCSCC + -- CCSSS|SSSCC + -- CCCCC|CCCCC + -- C = Concrete, S = Stainless Steel, W = water node (not floating), #=reactor core, |=HV cable + -- The man-hole and the HV cable is only in the middle + -- The man-hole is optional - --print("Concrete 1 ( 96):"..#inner_shield_nodes) - if #inner_shield_nodes ~= 96 then - --print("Inner shield defect") - return 0 - end - local steel_shield_nodes = minetest.find_nodes_in_area({x=pos.x-3, y=pos.y-3, z=pos.z-3}, - {x=pos.x+3, y=pos.y+3, z=pos.z+3}, - "default:steelblock") + local source_water_nodes = minetest.find_nodes_in_area( + {x=pos.x-1, y=pos.y-1, z=pos.z-1}, + {x=pos.x+1, y=pos.y+1, z=pos.z+1}, + "default:water_source") + local flowing_water_nodes = minetest.find_nodes_in_area( + {x=pos.x-1, y=pos.y-1, z=pos.z-1}, + {x=pos.x+1, y=pos.y+1, z=pos.z+1}, + "default:water_flowing") + if not ((#source_water_nodes + #flowing_water_nodes) >= 25) then + return false + end - --print("Steel ( 216):"..#steel_shield_nodes) - if #steel_shield_nodes ~= 216 then - --print("Steel shield defect") - return 0 - end - local outer_shield_nodes = minetest.find_nodes_in_area({x=pos.x-5, y=pos.y-5, z=pos.z-5}, - {x=pos.x+5, y=pos.y+5, z=pos.z+5}, - "technic:concrete") - --print("Concrete 2 (1080):"..#outer_shield_nodes) - if #outer_shield_nodes ~= (984+#inner_shield_nodes) then - --print("Outer shield defect") - return 0 - end - return 1 - end + local inner_shield_nodes = minetest.find_nodes_in_area( + {x=pos.x-2, y=pos.y-2, z=pos.z-2}, + {x=pos.x+2, y=pos.y+2, z=pos.z+2}, + "technic:concrete") + if not (#inner_shield_nodes >= 96) then + return false + end + + local steel_shield_nodes = minetest.find_nodes_in_area( + {x=pos.x-3, y=pos.y-3, z=pos.z-3}, + {x=pos.x+3, y=pos.y+3, z=pos.z+3}, + "default:steelblock") + if not (#steel_shield_nodes >= 216) then + return false + end + + local outer_shield_nodes = minetest.find_nodes_in_area( + {x=pos.x-5, y=pos.y-5, z=pos.z-5}, + {x=pos.x+5, y=pos.y+5, z=pos.z+5}, + "technic:concrete") + if not (#outer_shield_nodes >= (984 + #inner_shield_nodes)) then + return false + end + return true +end local explode_reactor = function(pos) - print("BOOM A reactor exploded!") - end + print("BOOM A reactor exploded!") +end -minetest.register_abm( - {nodenames = {"technic:hv_nuclear_reactor_core","technic:hv_nuclear_reactor_core_active"}, - interval = 1, - chance = 1, - action = function(pos, node, active_object_count, active_object_count_wider) +minetest.register_abm({ + nodenames = {"technic:hv_nuclear_reactor_core", "technic:hv_nuclear_reactor_core_active"}, + interval = 1, + chance = 1, + action = function(pos, node, active_object_count, active_object_count_wider) local meta = minetest.env:get_meta(pos) - local burn_time= meta:get_int("burn_time") + local burn_time = meta:get_int("burn_time") -- If more to burn and the energy produced was used: produce some more - if burn_time>0 then - if meta:get_int("HV_EU_supply") == 0 then - -- We did not use the power - meta:set_int("HV_EU_supply", power_sypply) - else - burn_time = burn_time - 1 - meta:set_int("burn_time",burn_time) - meta:set_string("infotext", "Nuclear Reactor Core ("..math.floor(burn_time/(burn_ticks*60)*100).."%)") - end + if burn_time > 0 then + if not check_reactor_structure(pos) then + explode_reactor(pos) + end + if meta:get_int("HV_EU_supply") == 0 then + -- We did not use the power + meta:set_int("HV_EU_supply", power_supply) + else + burn_time = burn_time - 1 + meta:set_int("burn_time", burn_time) + local percent = math.floor(burn_time / (burn_ticks * 60) * 100) + meta:set_string("infotext", "Nuclear Reactor Core ("..percent.."%)") + end end -- Burn another piece of coal - if burn_time==0 then - local inv = meta:get_inventory() - local correct_fuel_count = 0 - if inv:is_empty("src") == false then - local srclist= inv:get_list("src") - for _, srcstack in pairs(srclist) do - if srcstack then - local src_item=srcstack:to_table() - if src_item and src_item["name"] == fuel_type then - correct_fuel_count = correct_fuel_count + 1 - end - end - end - -- Check that the reactor is complete as well as the correct number of correct fuel - if correct_fuel_count == 6 then - if check_reactor_structure(pos) == 1 then - burn_time=burn_ticks*60 - meta:set_int("burn_time",burn_time) - hacky_swap_node (pos,"technic:hv_nuclear_reactor_core_active") - meta:set_int("HV_EU_supply", power_supply) - for idx, srcstack in pairs(srclist) do - srcstack:take_item() - inv:set_stack("src", idx, srcstack) - end - else - -- BOOM!!! (the reactor was compromised and it should explode after some time) TNT mod inspired?? - explode_reactor(pos) - end - else - meta:set_int("HV_EU_supply", 0) - end - end + if burn_time <= 0 then + local inv = meta:get_inventory() + local correct_fuel_count = 0 + if not inv:is_empty("src") then + local srclist = inv:get_list("src") + for _, srcstack in pairs(srclist) do + if srcstack then + local src_item=srcstack:to_table() + if src_item and src_item["name"] == fuel_type then + correct_fuel_count = correct_fuel_count + 1 + end + end + end + -- Check that the reactor is complete as well as the correct number of correct fuel + if correct_fuel_count == 6 then + if not check_reactor_structure(pos) then + burn_time = burn_ticks * 60 + meta:set_int("burn_time", burn_time) + hacky_swap_node (pos,"technic:hv_nuclear_reactor_core_active") + meta:set_int("HV_EU_supply", power_supply) + for idx, srcstack in pairs(srclist) do + srcstack:take_item() + inv:set_stack("src", idx, srcstack) + end + end + else + meta:set_int("HV_EU_supply", 0) + end + end end -- Nothing left to burn - if burn_time==0 then - meta:set_string("infotext", "Nuclear Reactor Core (idle)") - hacky_swap_node (pos,"technic:hv_nuclear_reactor_core") + if burn_time == 0 then + meta:set_string("infotext", "Nuclear Reactor Core (idle)") + hacky_swap_node(pos,"technic:hv_nuclear_reactor_core") end - end - }) + end +}) technic.register_HV_machine ("technic:hv_nuclear_reactor_core","PR") technic.register_HV_machine ("technic:hv_nuclear_reactor_core_active","PR") From 1f4c621ffcd22334026c4b109f1b399327c01260 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 12 Jul 2013 20:28:07 -0400 Subject: [PATCH 4/5] Fix code style of the high voltage battery box --- technic/machines/hv/battery_box.lua | 376 ++++++++++++++-------------- 1 file changed, 193 insertions(+), 183 deletions(-) diff --git a/technic/machines/hv/battery_box.lua b/technic/machines/hv/battery_box.lua index 979dc13..10b9e66 100644 --- a/technic/machines/hv/battery_box.lua +++ b/technic/machines/hv/battery_box.lua @@ -1,187 +1,194 @@ + +local max_charge = 1500000 +local max_charge_rate = 3000 +local max_discharge_rate = 5000 + -- HV battery box -minetest.register_craft( - {output = 'technic:hv_battery_box 1', - recipe = { - {'technic:mv_battery_box', 'technic:mv_battery_box', 'technic:mv_battery_box'}, - {'technic:mv_battery_box', 'technic:hv_transformer', 'technic:mv_battery_box'}, - {'', 'technic:hv_cable', ''}, - } - }) +minetest.register_craft({ + output = 'technic:hv_battery_box 1', + recipe = { + {'technic:mv_battery_box', 'technic:mv_battery_box', 'technic:mv_battery_box'}, + {'technic:mv_battery_box', 'technic:hv_transformer', 'technic:mv_battery_box'}, + {'', 'technic:hv_cable', ''}, + } +}) local 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;]" + "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) - local meta = minetest.env:get_meta(pos) +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) + 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) + meta:set_int("HV_EU_demand", 0) -- How much this node can charge + meta:set_int("HV_EU_supply", 0) -- How much this node can discharge + meta:set_int("HV_EU_input", 0) -- How much power this machine is getting. + meta:set_float("internal_EU_charge", 0) + 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("src") or not inv:is_empty("dst") then + minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); + return false + else + return true + end + 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, + drop="technic:hv_battery_box", + can_dig = function(pos,player) + 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) - meta:set_int("HV_EU_demand", 0) -- How much can this node charge - meta:set_int("HV_EU_supply", 0) -- How much can this node discharge - meta:set_int("HV_EU_input", 0) -- How much power is this machine getting. - meta:set_float("internal_EU_charge", 0) - 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("src") or not inv:is_empty("dst") then - minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); - return false - else - return true - end + if not inv:is_empty("src") or not inv:is_empty("dst") then + minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); + return false + else + return true + end 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, - drop="technic:hv_battery_box", - can_dig = function(pos,player) - local meta = minetest.env:get_meta(pos); - local inv = meta:get_inventory() - if not inv:is_empty("src") or not inv:is_empty("dst") then - minetest.chat_send_player(player:get_player_name(), "Machine cannot be removed because it is not empty"); - return false - else - return true - end - end, - }) + }) end local power_tools = technic.HV_power_tools -local charge_HV_tools = function(meta, charge) - --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 toolname = src_item["name"] - if power_tools[toolname] ~= nil then - -- Set meta data for the tool if it didn't do it itself :-( - src_meta=get_item_meta(src_item["metadata"]) - if src_meta==nil then - src_meta={} - src_meta["technic_hv_power_tool"]=true - src_meta["charge"]=0 - else - if src_meta["technic_hv_power_tool"]==nil then - src_meta["technic_hv_power_tool"]=true - src_meta["charge"]=0 - end - end - -- Do the charging - local item_max_charge = power_tools[toolname] - local load = src_meta["charge"] - local load_step = 1000 -- how much to charge per tick - if load0 then - if charge-load_step<0 then load_step=charge end - if load+load_step>item_max_charge then load_step=item_max_charge-load end - load=load+load_step - charge=charge-load_step - technic.set_RE_wear(src_item,load,item_max_charge) - src_meta["charge"] = load - src_item["metadata"] = set_item_meta(src_meta) - inv:set_stack("src", 1, src_item) - end - end - end - return charge -- return the remaining charge in the battery - end +local function charge_HV_tools(meta, charge) + --charge registered power tools + local inv = meta:get_inventory() + if not inv:is_empty("src") then + local srcstack = inv:get_stack("src", 1) + local src_item = srcstack:to_table() + local src_meta = get_item_meta(src_item["metadata"]) -local discharge_HV_tools = function(meta, charge, max_charge) - -- discharging registered power tools - local inv = meta:get_inventory() - 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 toolname = src_item["name"] - if power_tools[toolname] ~= nil then - -- Set meta data for the tool if it didn't do it itself :-( - src_meta=get_item_meta(src_item["metadata"]) - if src_meta==nil then - src_meta={} - src_meta["technic_hv_power_tool"]=true - src_meta["charge"]=0 - else - if src_meta["technic_hv_power_tool"]==nil then - src_meta["technic_hv_power_tool"]=true - src_meta["charge"]=0 - end - end - -- Do the discharging - local item_max_charge = power_tools[toolname] - local load = src_meta["charge"] - local load_step = 4000 -- how much to discharge per tick - if load>0 and chargemax_charge then load_step=max_charge-charge end - if load-load_step<0 then load_step=load end - load=load-load_step - charge=charge+load_step - technic.set_RE_wear(src_item,load,item_max_charge) - src_meta["charge"]=load - src_item["metadata"]=set_item_meta(src_meta) - inv:set_stack("dst", 1, src_item) - end - end + local toolname = src_item["name"] + if power_tools[toolname] ~= nil then + -- Set meta data for the tool if it didn't do it itself :-( + src_meta = get_item_meta(src_item["metadata"]) + if src_meta==nil then + src_meta = {} + src_meta["technic_hv_power_tool"] = true + src_meta["charge"] = 0 + else + if src_meta["technic_hv_power_tool"] == nil then + src_meta["technic_hv_power_tool"] = true + src_meta["charge"] = 0 + end end - return charge -- return the remaining charge in the battery - end + -- Do the charging + local item_max_charge = power_tools[toolname] + local tool_charge = src_meta["charge"] + local charge_step = 1000 -- how much to charge per tick + if tool_charge < item_max_charge and tool_charge > 0 then + if tool_charge - charge_step < 0 then + charge_step = charge + end + if tool_charge + charge_step > item_max_charge then + charge_step = item_max_charge - tool_charge + end + tool_charge = tool_charge + charge_step + charge = charge - charge_step + technic.set_RE_wear(src_item, tool_charge, item_max_charge) + src_meta["charge"] = tool_charge + src_item["metadata"] = set_item_meta(src_meta) + inv:set_stack("src", 1, src_item) + end + end + end + return charge -- return the remaining charge in the battery +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 -- Set maximum charge for the device here - local max_charge_rate = 3000 -- Set maximum rate of charging - local max_discharge_rate = 5000 -- Set maximum rate of discharging (16000) - local eu_input = meta:get_int("HV_EU_input") - local current_charge = meta:get_int("internal_EU_charge") -- Battery charge right now +local function discharge_HV_tools(meta, charge, max_charge) + -- discharging registered power tools + local inv = meta:get_inventory() + if not inv:is_empty("dst") then + srcstack = inv:get_stack("dst", 1) + src_item = srcstack:to_table() + local src_meta = get_item_meta(src_item["metadata"]) + local toolname = src_item["name"] + if power_tools[toolname] ~= nil then + -- Set meta data for the tool if it didn't do it itself :-( + src_meta = get_item_meta(src_item["metadata"]) or {} + if src_meta["technic_hv_power_tool"] == nil then + src_meta["technic_hv_power_tool"] = true + src_meta["charge"] = 0 + end + + -- Do the discharging + local item_max_charge = power_tools[toolname] + local tool_charge = src_meta["charge"] + local charge_step = 4000 -- how much to discharge per tick + if tool_charge > 0 and charge < max_charge then + if tool_charge + charge_step > max_charge then + charge_step = max_charge - charge + end + if tool_charge - charge_step < 0 then + charge_step = charge + end + tool_charge = tool_charge - charge_step + charge = charge + charge_step + technic.set_RE_wear(src_item, tool_charge, item_max_charge) + src_meta["charge"] = tool_charge + src_item["metadata"] = set_item_meta(src_meta) + inv:set_stack("dst", 1, src_item) + end + end + end + return charge -- return the remaining charge in the battery +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 eu_input = meta:get_int("HV_EU_input") + local current_charge = meta:get_int("internal_EU_charge") -- Battery charge right now -- Power off automatically if no longer connected to a switching station technic.switching_station_timeout_count(pos, "HV") -- Charge/discharge the battery with the input EUs - if eu_input >=0 then - current_charge = math.min(current_charge+eu_input, max_charge) + if eu_input >= 0 then + current_charge = math.min(current_charge + eu_input, max_charge) else - current_charge = math.max(current_charge+eu_input, 0) + current_charge = math.max(current_charge + eu_input, 0) end -- Charging/discharging tools here @@ -195,37 +202,40 @@ minetest.register_abm( meta:set_int("HV_EU_supply", math.min(max_discharge_rate, current_charge)) meta:set_int("internal_EU_charge", current_charge) - --dprint("BA: input:"..eu_input.." supply="..meta:get_int("HV_EU_supply").." demand="..meta:get_int("HV_EU_demand").." current:"..current_charge) -- Select node textures - local i=math.ceil((current_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) + local charge_count = math.ceil((current_charge / max_charge) * 8) + if charge_count > 8 then + charge_count = 8 + end + local last_count = meta:get_float("last_side_shown") + if charge_count ~= last_count then + if charge_count > 0 then + hacky_swap_node(pos,"technic:hv_battery_box"..charge_count) + else + hacky_swap_node(pos,"technic:hv_battery_box") + end + meta:set_float("last_side_shown", charge_count) end - local load = math.floor(current_charge/max_charge * 100) + local charge_percent = math.floor(current_charge / max_charge * 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]" - ) + battery_box_formspec.. + "image[1,1;1,2;technic_power_meter_bg.png^[lowpart:" + ..charge_percent..":technic_power_meter_fg.png]") if eu_input == 0 then - meta:set_string("infotext", "HV Battery box: "..current_charge.."/"..max_charge.." (idle)") + meta:set_string("infotext", "HV Battery box: "..current_charge.."/"..max_charge.." (idle)") else - meta:set_string("infotext", "HV Battery box: "..current_charge.."/"..max_charge) + meta:set_string("infotext", "HV Battery box: "..current_charge.."/"..max_charge) end - end - }) + end +}) -- Register as a battery type -- Battery type machines function as power reservoirs and can both receive and give back power technic.register_HV_machine("technic:hv_battery_box","BA") for i=1,8,1 do - technic.register_HV_machine("technic:hv_battery_box"..i,"BA") + technic.register_HV_machine("technic:hv_battery_box"..i,"BA") end From 57cd394773fcd95d296d7ee02e2ade3d62485be5 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Fri, 12 Jul 2013 20:32:38 -0400 Subject: [PATCH 5/5] Gigh -> High --- technic/machines/hv/wires.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/technic/machines/hv/wires.lua b/technic/machines/hv/wires.lua index b2c9834..47f714d 100644 --- a/technic/machines/hv/wires.lua +++ b/technic/machines/hv/wires.lua @@ -92,7 +92,7 @@ if z2==1 then temp_z2=str_z2 end minetest.register_node("technic:hv_cable"..count, { - description = "Gigh Voltage Copper Cable", + description = "High Voltage Copper Cable", tiles = {"technic_hv_cable.png"}, groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2,not_in_creative_inventory=1}, sounds = default.node_sound_wood_defaults(),