Forcefield now working with updated power scheme.

This commit is contained in:
kpoppel 2013-07-04 21:33:27 +02:00
parent 9770be430d
commit 9a0d3f2166
3 changed files with 109 additions and 94 deletions

View File

@ -1,12 +1,13 @@
technic 0.4.6 technic 0.4.7
Technic mod for Minetest 0.4.6 Technic mod for Minetest 0.4.7
Credits for contributing to the project: Credits for contributing to the project:
Nekogloop Nekogloop
ShadowNinja ShadowNinja
VanessaE VanessaE
Nore/Novatux Nore/Novatux
kpoppel
And many others for ideas/inspiring And many others for ideas/inspiring
Licences: Licences:

View File

@ -1,7 +1,9 @@
--Forcefield mod by ShadowNinja -- Forcefield mod by ShadowNinja
-- Modified by kpoppel
--
-- Forcefields are powerful barriers but they consume huge amounts of power.
-- Forcefield Generator is a HV machine.
local forcefield_emitter_buffer_size = 10000
local forcefield_emitter_power_consumption = 0.8
local forcefield_update_interval = 1 local forcefield_update_interval = 1
minetest.register_craft({ minetest.register_craft({
@ -13,22 +15,13 @@ minetest.register_craft({
} }
}) })
local function get_forcefield_count(range) -- Idea: Let forcefields have different colors by upgrade slot.
local count = 0 -- Idea: Let forcefields add up by detecting if one hits another.
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 (y == 0) or ((range-1) * (range-1) + (range-1) <= x*x+y*y+z*z) then --
count = count + 1
end
end
end
end
end
return count
end
local function add_forcefield(pos, range) local function add_forcefield(pos, range)
for x=-range,range do for x=-range,range do
for y=-range,range do for y=-range,range do
@ -66,69 +59,90 @@ local function remove_forcefield(p, range)
end end
end end
local forcefield_receive_fields = function(pos, formname, fields, sender) local get_forcefield_formspec = function(range)
local meta = minetest.env:get_meta(pos) -- return "invsize[8,9;]".. (if upgrades added later - colors for instance)
local range = meta:get_int("range") return "invsize[3,4;]"..
if fields.add then range = range + 1 end "label[0,0;Forcefield emitter]"..
if fields.subtract then range = range - 1 end "label[1,1;Range]"..
if fields.toggle then "label[1,2;"..range.."]"..
if meta:get_int("enabled") == 1 then "button[0,2;1,1;subtract;-]"..
meta:set_int("enabled", 0) "button[2,2;1,1;add;+]"..
else "button[0,3;3,1;toggle;Enable/Disable]" -- ..
meta:set_int("enabled", 1) -- "list[current_player;main;0,5;8,4;]"
end
end
if range <= 20 and range >= 0 and meta:get_int("range") ~= range then
remove_forcefield(pos, meta:get_int("range"))
meta:set_int("range", range)
local buffer = meta:get_float("internal_EU_buffer")
local buffer_size = meta:get_float("internal_EU_buffer_size")
local load = math.floor(buffer / buffer_size * 100)
meta:set_string("formspec", get_forcefield_formspec(range, 0))
end
end end
local get_forcefield_formspec = function(range, load) local forcefield_receive_fields = function(pos, formname, fields, sender)
if not load then load = 0 end local meta = minetest.env:get_meta(pos)
return "invsize[8,9;]".. local range = meta:get_int("range")
"label[0,0;Forcefield emitter]".. if fields.add then range = range + 1 end
"label[1,3;Power level]".. if fields.subtract then range = range - 1 end
"image[1,1;1,2;technic_power_meter_bg.png^[lowpart:".. if fields.toggle then
load..":technic_power_meter_fg.png]".. if meta:get_int("enabled") == 1 then
"label[4,1;Range]".. meta:set_int("enabled", 0)
"label[4,2;"..range.."]".. else
"button[3,2;1,1;add;+]".. meta:set_int("enabled", 1)
"button[5,2;1,1;subtract;-]".. end
"button[3,3;3,1;toggle;Enable/Disable]".. end
"list[current_player;main;0,5;8,4;]" -- Smallest field is 5. Anything less is asking for trouble.
end -- 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 forcefield_check = function(pos)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local internal_EU_buffer=meta:get_float("internal_EU_buffer") local eu_input = meta:get_int("HV_EU_input")
local internal_EU_buffer_size=meta:get_float("internal_EU_buffer_size") 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 load = math.floor(internal_EU_buffer/internal_EU_buffer_size * 100) local power_requirement
meta:set_string("formspec", get_forcefield_formspec(meta:get_int("range"), load)) if enabled == 1 then
power_requirement = math.floor(4*math.pi*math.pow(meta:get_int("range"), 2)) * 1
else
power_requirement = eu_demand
end
local power_requirement = get_forcefield_count(meta:get_int("range")) * forcefield_emitter_power_consumption if eu_input == 0 then
if meta:get_int("enabled") == 1 and internal_EU_buffer >= power_requirement then meta:set_string("infotext", "Forcefield Generator Unpowered")
if node.name == "technic:forcefield_emitter_off" then meta:set_int("HV_EU_demand", 100)
hacky_swap_node(pos, "technic:forcefield_emitter_on") if node.name == "technic:forcefield_emitter_on" then
end remove_forcefield(pos, meta:get_int("range"))
internal_EU_buffer=internal_EU_buffer-power_requirement; hacky_swap_node(pos, "technic:forcefield_emitter_off")
meta:set_float("internal_EU_buffer", internal_EU_buffer) meta:set_int("enabled", 0)
add_forcefield(pos, meta:get_int("range")) end
else elseif eu_input == power_requirement then
if node.name == "technic:forcefield_emitter_on" then if meta:get_int("enabled") == 1 then
remove_forcefield(pos, meta:get_int("range")) if node.name == "technic:forcefield_emitter_off" then
hacky_swap_node(pos, "technic:forcefield_emitter_off") hacky_swap_node(pos, "technic:forcefield_emitter_on")
end meta:set_string("infotext", "Forcefield Generator Active")
end add_forcefield(pos, meta:get_int("range"))
return true else
-- Range updated. Move the forcefield.
end 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 = { local mesecons = {effector = {
action_on = function(pos, node) action_on = function(pos, node)
@ -150,12 +164,12 @@ minetest.register_node("technic:forcefield_emitter_off", {
on_construct = function(pos) on_construct = function(pos)
minetest.env:get_node_timer(pos):start(forcefield_update_interval) minetest.env:get_node_timer(pos):start(forcefield_update_interval)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
meta:set_float("technic_power_machine", 1) meta:set_float("technic_hv_power_machine", 1)
meta:set_float("internal_EU_buffer", 0) meta:set_int("HV_EU_input", 0)
meta:set_float("internal_EU_buffer_size", forcefield_emitter_buffer_size) meta:set_int("HV_EU_demand", 0)
meta:set_int("range", 10) meta:set_int("range", 10)
meta:set_int("enabled", 1) meta:set_int("enabled", 0)
meta:set_string("formspec", get_forcefield_formspec(meta:get_int("range", 0))) meta:set_string("formspec", get_forcefield_formspec(meta:get_int("range")))
meta:set_string("infotext", "Forcefield emitter"); meta:set_string("infotext", "Forcefield emitter");
end, end,
mesecons = mesecons mesecons = mesecons
@ -172,19 +186,18 @@ minetest.register_node("technic:forcefield_emitter_on", {
on_construct = function(pos) on_construct = function(pos)
minetest.env:get_node_timer(pos):start(forcefield_update_interval) minetest.env:get_node_timer(pos):start(forcefield_update_interval)
local meta = minetest.env:get_meta(pos) local meta = minetest.env:get_meta(pos)
meta:set_float("technic_power_machine", 1) -- meta:set_float("technic_hv_power_machine", 1)
meta:set_float("internal_EU_buffer", 0) -- meta:set_float("HV_EU_input", 0)
meta:set_float("internal_EU_buffer_size", forcefield_emitter_buffer_size) -- meta:set_float("HV_EU_demand", 0)
meta:set_int("range", 10) -- meta:set_int("range", 10)
meta:set_int("enabled", 1) -- meta:set_int("enabled", 1)
meta:set_string("formspec", get_forcefield_formspec(meta:get_int("range"), 0)) meta:set_string("formspec", get_forcefield_formspec(meta:get_int("range")))
meta:set_string("infotext", "Forcefield emitter"); -- meta:set_string("infotext", "Forcefield emitter");
end, end,
on_dig = function(pos, node, digger) on_dig = function(pos, node, digger)
remove_forcefield(pos, minetest.env:get_meta(pos):get_int("range")) remove_forcefield(pos, minetest.env:get_meta(pos):get_int("range"))
return minetest.node_dig(pos, node, digger) return minetest.node_dig(pos, node, digger)
end, end,
technic_power_machine=1,
mesecons = mesecons mesecons = mesecons
}) })
@ -192,6 +205,7 @@ minetest.register_node("technic:forcefield", {
description = "Forcefield (you hacker you)", description = "Forcefield (you hacker you)",
sunlight_propagates = true, sunlight_propagates = true,
drop = '', 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, is_ground_content = true,
groups = {not_in_creative_inventory=1, unbreakable=1}, groups = {not_in_creative_inventory=1, unbreakable=1},
@ -205,5 +219,5 @@ minetest.register_node("technic:forcefield", {
}, },
}) })
technic.register_MV_machine("technic:forcefield_emitter_on","RE") technic.register_HV_machine("technic:forcefield_emitter_on","RE")
technic.register_MV_machine("technic:forcefield_emitter_off","RE") technic.register_HV_machine("technic:forcefield_emitter_off","RE")

View File

@ -51,7 +51,7 @@ dofile(modpath.."/battery_box_mv.lua")
dofile(modpath.."/solar_array_mv.lua") dofile(modpath.."/solar_array_mv.lua")
dofile(modpath.."/electric_furnace_mv.lua") dofile(modpath.."/electric_furnace_mv.lua")
dofile(modpath.."/alloy_furnace_mv.lua") dofile(modpath.."/alloy_furnace_mv.lua")
--dofile(modpath.."/forcefield.lua") dofile(modpath.."/forcefield.lua")
---- The power radiator supplies appliances with inductive coupled power: ---- The power radiator supplies appliances with inductive coupled power:
---- lighting and associated textures is taken directly from VanessaE's homedecor and made electric. ---- lighting and associated textures is taken directly from VanessaE's homedecor and made electric.
dofile(modpath.."/power_radiator.lua") dofile(modpath.."/power_radiator.lua")