Make it use tubelib2 for electric cables.

This commit is contained in:
SFENCE 2021-12-12 21:12:47 +01:00
parent 8f5011b269
commit 3e249d21e4
9 changed files with 180 additions and 6 deletions

View File

@ -49,6 +49,17 @@ if minetest.get_modpath("hades_technic") then
items.transformer = "hades_technic:lv_transformer"
end
minetest.register_craft({
output = "power_generators:electric_cableS 6",
recipe = {
{"basic_materials:plastic_sheet", "", ""},
{"", "basic_materials:copper_wire", ""},
{"", "", "basic_materials:plastic_sheet"},
},
replacements = {{items.copper_wire,items.empty_spool}},
})
minetest.register_craft({
output = "power_generators:combustion_engine_piston",
recipe = {

107
electric_network.lua Normal file
View File

@ -0,0 +1,107 @@
local S = power_generators.translator;
-- for lazy programmers
local S2P = minetest.string_to_pos
local P2S = minetest.pos_to_string
local Cable = tubelib2.Tube:new({
dirs_to_check = {1,2,3,4,5,6},
max_tube_length = 128,
show_infotext = false,
tube_type = "powgencable",
primary_node_names = {"power_generators:electric_cableS", "power_generators:electric_cableA",
},
after_place_tube = function(pos, param2, tube_type, num_tubes)
minetest.swap_node(pos, {name = "power_generators:electric_cable"..tube_type, param2 = param2})
end,
})
power_generators.electric_cable = Cable
local function gen_on_blast(drop)
return function(pos, intensity)
if intensity < 0.5 then
return {}
end
local node = minetest.get_node(pos)
minetest.remove_node(pos)
Cable:after_dig_tube(pos, node)
return {drop}
end
end
minetest.register_node("power_generators:electric_cableS", {
description = S("PG Electric Cable"),
tiles = {
-- up, down, right, left, back, front
"power_generators_electric_cable.png",
"power_generators_electric_cable.png",
"power_generators_electric_cable.png",
"power_generators_electric_cable.png",
"power_generators_electric_cable_end.png",
"power_generators_electric_cable_end.png",
},
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-1/16, -1/16, -1/2, 1/16, 1/16, 1/2},
},
},
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, power_generator_cable = 1},
sounds = default.node_sound_defaults(),
after_place_node = function(pos, placer, itemstack, pointed_thing)
if not Cable:after_place_tube(pos, placer, pointed_thing) then
minetest.remove_node(pos)
return true
end
return false
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Cable:after_dig_tube(pos, oldnode, oldmetadata)
end,
on_rotate = screwdriver.disallow, -- important!
on_blast = gen_on_blast("power_generators:electric_cableS"),
})
minetest.register_node("power_generators:electric_cableA", {
description = S("PG Electric Cable"),
tiles = {
-- up, down, right, left, back, front
"power_generators_electric_cable.png",
"power_generators_electric_cable_end.png",
"power_generators_electric_cable.png",
"power_generators_electric_cable.png",
"power_generators_electric_cable.png",
"power_generators_electric_cable_end.png",
},
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-1/16, -1/2, -1/16, 1/16, 1/16, 1/16},
{-1/16, -1/16, -1/2, 1/16, 1/16, -1/16},
},
},
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3,
techage_trowel = 1, not_in_creative_inventory = 1},
sounds = default.node_sound_defaults(),
drop = "power_generators:electric_cableS",
after_dig_node = function(pos, oldnode, oldmetadata, digger)
Cable:after_dig_tube(pos, oldnode)
end,
on_rotate = screwdriver.disallow, -- important!
on_blast = gen_on_blast("power_generators:electric_cableS"),
})

View File

@ -6,6 +6,7 @@
-- Initial Functions --
-----------------------
local S = power_generators.translator;
local Cable = power_generators.electric_cable
power_generators.emergency_generator = appliances.appliance:new(
{
@ -176,6 +177,11 @@ local node_active = {
emergency_generator:register_nodes(node_def, node_inactive, node_active)
Cable:add_secondary_node_names({emergency_generator.node_name_active, emergency_generator.node_name_inactive})
--Cable:add_special_node_names({name})
Cable:set_valid_sides(emergency_generator.node_name_active, {"R", "L", "F", "B"})
Cable:set_valid_sides(emergency_generator.node_name_inactive, {"R", "L", "F", "B"})
-------------------------
-- Recipe Registration --
-------------------------

View File

@ -1,13 +1,35 @@
local Cable = power_generators.electric_cable
local tubelib2_side = {
right = "R",
left = "L",
front = "F",
back = "B",
top = "U",
bottom = "D",
}
local function get_connected_node(pos, side)
local _,node = Cable:get_node(pos)
local dir = tubelib2.side_to_dir(side, node.param2)
local epos, edir = Cable:get_connected_node_pos(pos, dir)
if not (epos and edir) then
return nil
end
return epos
end
function power_generators.update_generator_supply(self, pos, use_usage)
local side_data = {};
local total_demand = 0;
for _,side in pairs(self.power_connect_sides) do
local side_pos = appliances.get_side_pos(pos, side);
local side_node = minetest.get_node(side_pos);
local side_def = minetest.registered_nodes[side_node.name];
if side_def and side_def._generator_connect_sides then
if appliances.is_connected_to(side_pos, pos, side_def._generator_connect_sides) then
local side_pos = get_connected_node(pos, tubelib2_side[side])
if side_pos then
local side_node = minetest.get_node(side_pos);
print(dump(side_node))
local side_def = minetest.registered_nodes[side_node.name];
if side_def and side_def._generator_connect_sides then
local meta = minetest.get_meta(side_pos);
local demand = meta:get_int("generator_demand") or 0
if (demand>0) then

View File

@ -5,7 +5,9 @@ power_generators = {
local modpath = minetest.get_modpath(minetest.get_current_modname())
dofile(modpath.."/electric_network.lua")
dofile(modpath.."/power_supply.lua")
--dofile(modpath.."/electric_junction.lua")
appliances.register_craft_type("power_generators_fuel", {
description = "Generator fuel",

View File

@ -1,5 +1,5 @@
name = power_generators
title = Add some power generators
description = Add power generators based on appliances API.
depends = appliances
depends = appliances, tubelib2, screwdriver
optional_depends = default, biofuel, basic_materials, hades_sounds, hades_biofuel, hades_extramaterials, sounds

View File

@ -1,6 +1,17 @@
-- register power supply to power generators
local Cable = power_generators.electric_cable
local tubelib2_side = {
right = "R",
left = "L",
front = "F",
back = "B",
top = "U",
bottom = "D",
}
local power_supply = {
is_powered = function (self, power_data, pos, meta)
local eu_input = meta:get_int("generator_input");
@ -21,6 +32,21 @@ local power_supply = {
node_def.groups.generator_powered = 1;
node_def._generator_connect_sides = self.power_connect_sides;
end,
after_register_node = function(self, power_data)
local names = {self.node_name_active, self.node_name_inactive}
if self.node_name_waiting then
table.insert(names, self.node_name_waiting)
end
local sides = {}
for _,side in pairs(self.power_connect_sides) do
table.insert(sides, tubelib2_side[side])
end
for _,name in pairs(names) do
Cable:add_secondary_node_names({name})
--Cable:add_special_node_names({name})
Cable:set_valid_sides(name, sides)
end
end,
}
appliances.add_power_supply("power_generators_power", power_supply)

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB