power distribution improved

This commit is contained in:
Joachim Stolberg 2019-06-20 14:01:37 +02:00
parent 3a9e889d57
commit b2d15efd27
7 changed files with 82 additions and 34 deletions

View File

@ -193,7 +193,6 @@ function techage.register_consumer(base_name, inv_name, tiles, tNode, validState
on_metadata_inventory_put = tNode.on_metadata_inventory_put,
on_metadata_inventory_take = tNode.on_metadata_inventory_take,
drop = "",
paramtype2 = "facedir",
groups = table.copy(tNode.groups),
is_ground_content = false,
@ -222,6 +221,7 @@ function techage.register_consumer(base_name, inv_name, tiles, tNode, validState
on_metadata_inventory_take = tNode.on_metadata_inventory_take,
paramtype2 = "facedir",
drop = "",
diggable = false,
groups = tNode.groups,
is_ground_content = false,

View File

@ -124,10 +124,31 @@ function techage.can_node_dig(node, ndef)
return true
end
local function handle_drop(drop)
-- To keep it simple, return only the item with the lowest rarity
if drop.items then
local rarity = 9999
local name
for idx,item in ipairs(drop.items) do
if item.rarity and item.rarity < rarity then
rarity = item.rarity
name = item.items[1] -- take always the first item
else
return item.items[1] -- take always the first item
end
end
return name
end
return false
end
-- returns the node name, if node can be dropped, otherwise nil
function techage.dropped_node(node, ndef)
if node.name == "air" then return end
if ndef.buildable_to == true then return end
if ndef.drop == "" then return end
if type(ndef.drop) == "table" then
return handle_drop(ndef.drop)
end
return ndef.drop or node.name
end

View File

@ -39,18 +39,18 @@ end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
--print("firebox burn_cycles = "..(mem.burn_cycles or 0))
print("firebox burn_cycles = "..(mem.burn_cycles or 0))
if mem.running then
-- trigger generator and provide power ratio 0..1
local ratio = techage.transfer(
{x=pos.x, y=pos.y+2, z=pos.z},
nil, -- outdir
"trigger", -- topic
mem.power_level/4.0, -- payload
(mem.power_level or 4)/4.0, -- payload
nil, -- network
{"techage:coalboiler_top"} -- nodenames
)
mem.burn_cycles = (mem.burn_cycles or 0) - math.max((ratio or 0.1), 0.1)
mem.burn_cycles = (mem.burn_cycles or 0) - math.max((ratio or 0.02), 0.02)
if mem.burn_cycles <= 0 then
local taken = firebox.get_fuel(pos)
if taken then

View File

@ -4,7 +4,7 @@ local P = minetest.string_to_pos
local M = minetest.get_meta
local PWR_NEEDED = 0.5
local CYCLE_TIME = 2
local CYCLE_TIME = 4
local Cable = techage.ElectricCable
local consume_power = techage.power.consume_power
@ -20,20 +20,29 @@ local function swap_node(pos, postfix)
minetest.swap_node(pos, node)
end
local function node_timer(pos, elapsed)
--print("node_timer lamp "..S(pos))
local function on_power(pos)
local mem = tubelib2.get_mem(pos)
if mem.running then
if mem.turned_on then
local got = consume_power(pos, PWR_NEEDED)
if got < PWR_NEEDED then
if got < PWR_NEEDED and mem.node_on then
swap_node(pos, "off")
else
mem.node_on = false
elseif not mem.node_on then
swap_node(pos, "on")
mem.node_on = true
end
return true
mem.trigger = true
end
swap_node(pos, "off")
return false
end
local function node_timer(pos, elapsed)
local mem = tubelib2.get_mem(pos)
if mem.node_on and not mem.trigger then
mem.node_on = false
swap_node(pos, "off")
end
mem.trigger = false
return mem.turned_on
end
local function lamp_on_rightclick(pos, node, clicker)
@ -41,14 +50,20 @@ local function lamp_on_rightclick(pos, node, clicker)
return
end
local mem = tubelib2.get_mem(pos)
if not mem.running and power_available(pos, PWR_NEEDED) then
mem.running = true
mem.turned_on = not mem.turned_on
if mem.turned_on and power_available(pos, PWR_NEEDED) then
mem.node_on = true
swap_node(pos, "on")
mem.trigger = true
minetest.get_node_timer(pos):start(CYCLE_TIME)
elseif mem.turned_on then
mem.node_on = false
swap_node(pos, "off")
minetest.get_node_timer(pos):start(CYCLE_TIME)
else
mem.running = false
minetest.get_node_timer(pos):stop()
mem.node_on = false
swap_node(pos, "off")
minetest.get_node_timer(pos):stop()
end
end
@ -105,5 +120,6 @@ function techage.register_lamp(basename, ndef_off, ndef_on)
techage.power.register_node({basename.."_off", basename.."_on"}, {
power_network = Cable,
conn_sides = ndef_off.conn_sides or determine_power_side, -- will be handled by clbk function
on_power = on_power,
})
end

View File

@ -19,6 +19,9 @@ local M = minetest.get_meta
local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua")
local Cable = techage.ElectricCable
local power_switched = techage.power.power_switched
local size = 3/32
local Boxes = {
{{-size, -size, size, size, size, 0.5 }}, -- z+
@ -29,7 +32,7 @@ local Boxes = {
{{-size, -size, -size, size, 0.5, size}}, -- y+
}
techage.register_junction("techage:electric_junction", 2/8, Boxes, techage.ElectricCable, {
techage.register_junction("techage:electric_junction", 2/8, Boxes, Cable, {
description = I("TA Electric Junction Box"),
tiles = {"techage_electric_junction.png"},
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, techage_trowel = 1},
@ -37,9 +40,9 @@ techage.register_junction("techage:electric_junction", 2/8, Boxes, techage.Elect
on_construct = tubelib2.init_mem,
after_tube_update = function(node, pos, out_dir, peer_pos, peer_in_dir)
local name = "techage:electric_junction"..techage.junction_type(pos, techage.ElectricCable)
local name = "techage:electric_junction"..techage.junction_type(pos, Cable)
minetest.swap_node(pos, {name = name, param2 = 0})
techage.power.on_network_change(pos)
power_switched(pos)
end,
})

View File

@ -143,6 +143,16 @@ local function store_master(pos, master_pos)
end)
end
local function trigger_lamps(pos)
Route = {}
pos_already_reached(pos)
connection_walk(pos, function(pos, mem)
local pwr = PWR(pos)
if pwr and pwr.on_power then
pwr.on_power(pos)
end
end)
end
-- called from any generator
local function on_power_switch(pos)
@ -154,7 +164,6 @@ local function on_power_switch(pos)
local mpos = determine_master(pos)
store_master(pos, mpos)
if mpos then
--print("master = "..S(mpos))
local mem = tubelib2.get_mem(mpos)
mem.is_master = true
return mem
@ -189,21 +198,12 @@ local function accounting(mem)
mem.available2 = 0
end
-- called from tubelib2.after_tube_update
local function on_network_change(pos)
local mem = on_power_switch(pos)
if mem then
accounting(mem)
end
end
--
-- Generic API functions
--
techage.power = {}
techage.power.power_switched = on_power_switch
techage.power.on_network_change = on_network_change
-- Used to turn on/off the power by means of a power switch
function techage.power.power_cut(pos, dir, cable, cut)
@ -223,6 +223,7 @@ function techage.power.power_cut(pos, dir, cable, cut)
for dir,_ in pairs(mem.connections) do
mem.interrupted_dirs[dir] = false
on_power_switch(npos)
trigger_lamps(npos)
mem.interrupted_dirs[dir] = true
end
else
@ -238,6 +239,7 @@ function techage.power.register_node(names, pwr_def)
minetest.override_item(name, {
power = {
conn_sides = pwr_def.conn_sides or {"L", "R", "U", "D", "F", "B"},
on_power = pwr_def.on_power,
power_network = pwr_def.power_network,
after_place_node = ndef.after_place_node,
after_dig_node = ndef.after_dig_node,
@ -274,7 +276,7 @@ function techage.power.register_node(names, pwr_def)
mem.connections[out_dir] = {pos = peer_pos, in_dir = peer_in_dir}
end
-- To be called delayed, so that all network connections have been established
minetest.after(0.2, on_network_change, pos)
minetest.after(0.2, on_power_switch, pos)
if pwr.after_tube_update then
return pwr.after_tube_update(node, pos, out_dir, peer_pos, peer_in_dir)
end
@ -304,6 +306,7 @@ function techage.power.provide_power(pos, provide)
local mem = tubelib2.get_mem(pos)
if mem.is_master then
accounting(mem)
trigger_lamps(pos, mem)
elseif mem.master_pos then
mem = tubelib2.get_mem(mem.master_pos)
else
@ -322,6 +325,7 @@ function techage.power.secondary_power(pos, provide, needed)
local mem = tubelib2.get_mem(pos)
if mem.is_master then
accounting(mem)
trigger_lamps(pos, mem)
elseif mem.master_pos then
mem = tubelib2.get_mem(mem.master_pos)
else

View File

@ -21,6 +21,10 @@ local M = minetest.get_meta
local MP = minetest.get_modpath("techage")
local I,_ = dofile(MP.."/intllib.lua")
local Cable = techage.ElectricCable
local power_cut = techage.power.power_cut
local Param2ToDir = {
[0] = 6,
[1] = 5,
@ -42,7 +46,7 @@ local function switch_on(pos, node, clicker)
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
techage.power.power_cut(pos, dir, techage.ElectricCable, false)
power_cut(pos, dir, Cable, false)
end
local function switch_off(pos, node, clicker)
@ -58,7 +62,7 @@ local function switch_off(pos, node, clicker)
max_hear_distance = 5,
})
local dir = Param2ToDir[node.param2]
techage.power.power_cut(pos, dir, techage.ElectricCable, true)
power_cut(pos, dir, Cable, true)
end
@ -167,7 +171,7 @@ minetest.register_node("techage:powerswitch_box", {
})
techage.power.register_node({"techage:powerswitch_box"}, {
power_network = techage.ElectricCable})
power_network = Cable})
minetest.register_craft({
output = "techage:powerswitch 2",