techage/power/electric_cable.lua

180 lines
5.2 KiB
Lua
Raw Normal View History

2019-03-02 14:24:48 +03:00
--[[
TechAge
=======
2020-01-26 01:15:44 +03:00
Copyright (C) 2019-2020 Joachim Stolberg
2019-03-02 14:24:48 +03:00
GPL v3
2019-03-02 14:24:48 +03:00
See LICENSE.txt for more information
2020-01-26 01:15:44 +03:00
TA3 Electric Cables (AC)
2019-03-02 14:24:48 +03:00
]]--
-- for lazy programmers
local S2P = minetest.string_to_pos
local P2S = minetest.pos_to_string
2019-03-02 14:24:48 +03:00
local M = minetest.get_meta
local S = techage.S
2019-03-02 14:24:48 +03:00
2020-01-26 22:15:40 +03:00
local power = techage.power
2020-01-26 01:15:44 +03:00
local ELE1_MAX_CABLE_LENGHT = 1000
2019-03-02 14:24:48 +03:00
local Cable = tubelib2.Tube:new({
dirs_to_check = {1,2,3,4,5,6},
2020-01-26 01:15:44 +03:00
max_tube_length = ELE1_MAX_CABLE_LENGHT,
2019-03-02 14:24:48 +03:00
show_infotext = false,
2020-01-26 01:15:44 +03:00
tube_type = "ele1",
primary_node_names = {"techage:electric_cableS", "techage:electric_cableA",
"techage:power_line", "techage:power_lineS", "techage:power_lineA",
2020-01-26 22:15:40 +03:00
"techage:power_pole2", "techage:powerswitch_box"},
2019-05-11 02:21:03 +03:00
secondary_node_names = {},
2019-03-02 14:24:48 +03:00
after_place_tube = function(pos, param2, tube_type, num_tubes)
-- Handle "power line" nodes
local name = minetest.get_node(pos).name
if name == "techage:power_pole2" then
M(pos):set_int("tl2_param2", param2)
return
2020-01-26 22:15:40 +03:00
elseif name == "techage:powerswitch_box" then
2020-02-25 21:43:27 +03:00
minetest.swap_node(pos, {name = "techage:powerswitch_box", param2 = param2 % 32})
2020-01-26 22:15:40 +03:00
M(pos):set_int("tl2_param2", param2)
return
elseif name == "techage:power_line" or name == "techage:power_lineS" or name == "techage:power_lineA" then
minetest.swap_node(pos, {name = "techage:power_line"..tube_type, param2 = param2 % 32})
2019-06-22 00:04:27 +03:00
M(pos):set_int("tl2_param2", param2)
return
end
2019-05-22 00:41:17 +03:00
-- Don't replace "hidden" cable
if M(pos):get_string("techage_hidden_nodename") == "" then
minetest.swap_node(pos, {name = "techage:electric_cable"..tube_type, param2 = param2 % 32})
end
2019-03-02 14:24:48 +03:00
M(pos):set_int("tl2_param2", param2)
end,
})
-- Overridden method of tubelib2!
function Cable:get_primary_node_param2(pos, dir)
return techage.get_primary_node_param2(pos, dir)
end
function Cable:is_primary_node(pos, dir)
return techage.is_primary_node(pos, dir)
end
function Cable:get_secondary_node(pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local node = self:get_node_lvm(npos)
if self.secondary_node_names[node.name] or
self.secondary_node_names[M(npos):get_string("techage_hidden_nodename")] then
return node, npos
end
end
function Cable:is_secondary_node(pos, dir)
local npos = vector.add(pos, tubelib2.Dir6dToVector[dir or 0])
local node = self:get_node_lvm(npos)
return self.secondary_node_names[node.name] or
self.secondary_node_names[M(npos):get_string("techage_hidden_nodename")]
end
2019-03-02 14:24:48 +03:00
minetest.register_node("techage:electric_cableS", {
description = S("TA Electric Cable"),
2019-03-02 14:24:48 +03:00
tiles = {
-- up, down, right, left, back, front
"techage_electric_cable.png",
"techage_electric_cable.png",
"techage_electric_cable.png",
"techage_electric_cable.png",
"techage_electric_cable_end.png",
"techage_electric_cable_end.png",
},
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)
if oldmetadata and oldmetadata.fields and oldmetadata.fields.tl2_param2 then
oldnode.param2 = oldmetadata.fields.tl2_param2
Cable:after_dig_tube(pos, oldnode)
end
end,
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-3/32, -3/32, -4/8, 3/32, 3/32, 4/8},
},
},
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3, techage_trowel = 1},
sounds = default.node_sound_defaults(),
})
minetest.register_node("techage:electric_cableA", {
description = S("TA Electric Cable"),
2019-03-02 14:24:48 +03:00
tiles = {
-- up, down, right, left, back, front
"techage_electric_cable.png",
"techage_electric_cable_end.png",
"techage_electric_cable.png",
"techage_electric_cable.png",
"techage_electric_cable.png",
"techage_electric_cable_end.png",
},
after_dig_node = function(pos, oldnode, oldmetadata, digger)
if oldmetadata and oldmetadata.fields and oldmetadata.fields.tl2_param2 then
oldnode.param2 = oldmetadata.fields.tl2_param2
Cable:after_dig_tube(pos, oldnode)
end
end,
paramtype2 = "facedir", -- important!
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {
{-3/32, -4/8, -3/32, 3/32, 3/32, 3/32},
{-3/32, -3/32, -4/8, 3/32, 3/32, -3/32},
},
},
on_rotate = screwdriver.disallow, -- important!
paramtype = "light",
sunlight_propagates = true,
is_ground_content = false,
2019-04-28 22:34:21 +03:00
groups = {snappy = 2, choppy = 2, oddly_breakable_by_hand = 3,
techage_trowel = 1, not_in_creative_inventory = 1},
2019-03-02 14:24:48 +03:00
sounds = default.node_sound_defaults(),
drop = "techage:electric_cableS",
})
2020-01-26 22:15:40 +03:00
-- only needed for hidden nodes, cause they don't have a tubelib2_on_update2 callback
Cable:register_on_tube_update(function(node, pos, out_dir, peer_pos, peer_in_dir)
power.update_network(pos, nil, Cable)
end)
2019-05-11 02:21:03 +03:00
minetest.register_craft({
output = "techage:electric_cableS 6",
recipe = {
{"basic_materials:plastic_sheet", "", ""},
{"", "default:copper_ingot", ""},
{"", "", "basic_materials:plastic_sheet"},
},
})
2020-01-26 01:15:44 +03:00
techage.ElectricCable = Cable
techage.ELE1_MAX_CABLE_LENGHT = ELE1_MAX_CABLE_LENGHT