diff --git a/cables.lua b/cables.lua index c5a8fea..668608b 100644 --- a/cables.lua +++ b/cables.lua @@ -15,27 +15,43 @@ -- along with this program. If not, see . local S=minetest.get_translator("industrialtest") -local cable={} -cable.onConstruct=function(pos) +industrialtest.Cable={} + +function industrialtest.Cable.onConstruct(self,pos) local connections=industrialtest.api.getConnections(pos) for _,conn in ipairs(connections) do local meta=minetest.get_meta(conn) if industrialtest.api.isNetworkMaster(meta) then industrialtest.api.createNetworkMapForNode(conn) + local networkNode=minetest.get_node(conn) + local def=minetest.registered_nodes[networkNode.name] + if def and def._industrialtest_self then + def._industrialtest_self:triggerIfNeeded(conn) + else + -- Support for bare definitions that don't use industrialtest pseudo-OOP + minetest.get_node_timer(conn):start(industrialtest.config.updateDelay) + end else local networks=industrialtest.api.isAttachedToNetwork(meta) if networks then for _,network in ipairs(networks) do industrialtest.api.createNetworkMapForNode(network) - minetest.get_node_timer(network):start(industrialtest.updateDelay) + local networkNode=minetest.get_node(network) + local def=minetest.registered_nodes[networkNode.name] + if def and def._industrialtest_self then + def._industrialtest_self:triggerIfNeeded(network) + else + -- Support for bare definitions that don't use industrialtest pseudo-OOP + minetest.get_node_timer(network):start(industrialtest.config.updateDelay) + end end end end end end -cable.onDestruct=function(pos) +function industrialtest.Cable.onDestruct(self,pos) local meta=minetest.get_meta(pos) local networks=industrialtest.api.isAttachedToNetwork(meta) if networks then @@ -45,14 +61,16 @@ cable.onDestruct=function(pos) end end -local function registerCable(name,displayName,size,flow,registerInsulated) - local definition={ - description=S(displayName.." Cable"), - inventory_image="industrialtest_"..name.."_cable_inv.png", - tiles={"industrialtest_"..name.."_cable.png"}, - wield_image="industrialtest_"..name.."_cable_inv.png", +function industrialtest.Cable.createDefinitionTable(self,description,inventoryImage,tile,insulated) + local size=(insulated and self.size+0.02 or self.size) + local def={ + description=description, + inventory_image=inventoryImage, + wield_image=inventoryImage, + tiles={tile}, paramtype="light", sunlight_propagates=true, + use_texture_alpha=(self.transparent and "clip" or "opaque"), drawtype="nodebox", node_box={ type="connected", @@ -118,39 +136,64 @@ local function registerCable(name,displayName,size,flow,registerInsulated) "group:_industrialtest_hasPowerOutput", "group:_industrialtest_cable" }, - on_construct=cable.onConstruct, - on_destruct=cable.onDestruct, - _industrialtest_cableFlow=flow + on_construct=function(pos) + self:onConstruct(pos) + end, + on_destruct=function(pos) + self:onDestruct(pos) + end, + _industrialtest_cableFlow=self.flow } + if industrialtest.mtgAvailable then - definition.groups={ + def.groups={ cracky=1, level=1, oddly_breakable_by_hand=1 } - definition.sound=default.node_sound_metal_defaults() + def.sound=default.node_sound_metal_defaults() elseif industrialtest.mclAvailable then - definition.groups={ + def.groups={ handy=1, pickaxey=1 } - definition._mcl_blast_resistance=1 - definition._mcl_hardness=0.5 - definition.sound=mcl_sounds.node_sound_metal_defaults() + def._mcl_blast_resistance=1 + def._mcl_hardness=0.5 + def.sound=mcl_sounds.node_sound_metal_defaults() end - definition.groups._industrialtest_cable=1 - minetest.register_node("industrialtest:"..name.."_cable",definition) - if registerInsulated then - definition=table.copy(definition) - definition.description=S("Insulated "..displayName.." Cable") - definition.inventory_image="industrialtest_insulated_"..name.."_cable_inv.png" - definition.tiles={"industrialtest_insulated_"..name.."_cable.png"} - definition.wield_image="industrialtest_insulated_"..name.."_cable_inv.png" - minetest.register_node("industrialtest:insulated_"..name.."_cable",definition) + def.groups._industrialtest_cable=1 + + return def +end + +function industrialtest.Cable.register(self) + local def=self:createDefinitionTable(self.description,self.inventoryImage,self.tile,false) + minetest.register_node(self.name,def) + + if self.insulated then + def=self:createDefinitionTable(self.insulated.description,self.insulated.inventoryImage,self.insulated.tile,true) + minetest.register_node(self.insulated.name,def) end end -registerCable("tin","Tin",0.19,industrialtest.api.lvPowerFlow,true) +industrialtest.TinCable=table.copy(industrialtest.Cable) +industrialtest.internal.unpackTableInto(industrialtest.TinCable,{ + name="industrialtest:tin_cable", + description=S("Tin Cable"), + inventoryImage="industrialtest_tin_cable_inv.png", + tile="industrialtest_tin_cable.png", + size=0.19, + flow=industrialtest.api.lvPowerFlow, + insulated={ + name="industrialtest:insulated_tin_cable", + description=S("Insulated Tin Cable"), + inventoryImage="industrialtest_insulated_tin_cable_inv.png", + tile="industrialtest_insulated_tin_cable.png" + } +}) + +industrialtest.TinCable:register() + minetest.register_craft({ type="shaped", output="industrialtest:tin_cable 6", @@ -158,6 +201,7 @@ minetest.register_craft({ {industrialtest.elementKeys.tinIngot,industrialtest.elementKeys.tinIngot,industrialtest.elementKeys.tinIngot} } }) + minetest.register_craft({ type="shapeless", output="industrialtest:insulated_tin_cable", @@ -166,6 +210,7 @@ minetest.register_craft({ industrialtest.elementKeys.rubber } }) + minetest.register_craft({ type="shaped", output="industrialtest:insulated_tin_cable 6", @@ -175,13 +220,31 @@ minetest.register_craft({ {industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber} } }) + industrialtest.api.registerCableFormerRecipe({ output="industrialtest:tin_cable 12", recipe=industrialtest.elementKeys.tinIngot, time=1 }) -registerCable("copper","Copper",0.15,industrialtest.api.mvPowerFlow,true) +industrialtest.CopperCable=table.copy(industrialtest.Cable) +industrialtest.internal.unpackTableInto(industrialtest.CopperCable,{ + name="industrialtest:copper_cable", + description=S("Copper Cable"), + inventoryImage="industrialtest_copper_cable_inv.png", + tile="industrialtest_copper_cable.png", + size=0.15, + flow=industrialtest.api.mvPowerFlow, + insulated={ + name="industrialtest:insulated_copper_cable", + description=S("Insulated Copper Cable"), + inventoryImage="industrialtest_insulated_copper_cable_inv.png", + tile="industrialtest_insulated_copper_cable.png" + } +}) + +industrialtest.CopperCable:register() + minetest.register_craft({ type="shaped", output="industrialtest:copper_cable 6", @@ -189,6 +252,7 @@ minetest.register_craft({ {industrialtest.elementKeys.copperIngot,industrialtest.elementKeys.copperIngot,industrialtest.elementKeys.copperIngot} } }) + minetest.register_craft({ type="shapeless", output="industrialtest:insulated_copper_cable", @@ -197,6 +261,7 @@ minetest.register_craft({ industrialtest.elementKeys.rubber } }) + minetest.register_craft({ type="shaped", output="industrialtest:insulated_copper_cable 6", @@ -206,12 +271,30 @@ minetest.register_craft({ {industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber} } }) + industrialtest.api.registerCableFormerRecipe({ output="industrialtest:copper_cable 12", recipe=industrialtest.elementKeys.copperIngot }) -registerCable("gold","Gold",0.15,industrialtest.api.hvPowerFlow,true) +industrialtest.GoldCable=table.copy(industrialtest.Cable) +industrialtest.internal.unpackTableInto(industrialtest.GoldCable,{ + name="industrialtest:gold_cable", + description=S("Gold Cable"), + inventoryImage="industrialtest_gold_cable_inv.png", + tile="industrialtest_gold_cable.png", + size=0.15, + flow=industrialtest.api.hvPowerFlow, + insulated={ + name="industrialtest:insulated_gold_cable", + description=S("Insulated Gold Cable"), + inventoryImage="industrialtest_insulated_gold_cable_inv.png", + tile="industrialtest_insulated_gold_cable.png" + } +}) + +industrialtest.GoldCable:register() + minetest.register_craft({ type="shaped", output="industrialtest:gold_cable 6", @@ -219,6 +302,7 @@ minetest.register_craft({ {industrialtest.elementKeys.goldIngot,industrialtest.elementKeys.goldIngot,industrialtest.elementKeys.goldIngot} } }) + minetest.register_craft({ type="shapeless", output="industrialtest:insulated_gold_cable", @@ -227,6 +311,7 @@ minetest.register_craft({ industrialtest.elementKeys.rubber } }) + minetest.register_craft({ type="shaped", output="industrialtest:insulated_gold_cable 6", @@ -236,12 +321,30 @@ minetest.register_craft({ {industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber} } }) + industrialtest.api.registerCableFormerRecipe({ output="industrialtest:gold_cable 12", recipe=industrialtest.elementKeys.goldIngot }) -registerCable("iron","Iron",0.15,industrialtest.api.evPowerFlow,true) +industrialtest.IronCable=table.copy(industrialtest.Cable) +industrialtest.internal.unpackTableInto(industrialtest.IronCable,{ + name="industrialtest:iron_cable", + description=S("Iron Cable"), + inventoryImage="industrialtest_iron_cable_inv.png", + tile="industrialtest_iron_cable.png", + size=0.15, + flow=industrialtest.api.evPowerFlow, + insulated={ + name="industrialtest:insulated_iron_cable", + description=S("Insulated Iron Cable"), + inventoryImage="industrialtest_insulated_iron_cable_inv.png", + tile="industrialtest_insulated_iron_cable.png" + } +}) + +industrialtest.IronCable:register() + minetest.register_craft({ type="shaped", output="industrialtest:iron_cable 6", @@ -249,6 +352,7 @@ minetest.register_craft({ {"industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot","industrialtest:refined_iron_ingot"} } }) + minetest.register_craft({ type="shapeless", output="industrialtest:insulated_iron_cable", @@ -257,6 +361,7 @@ minetest.register_craft({ industrialtest.elementKeys.rubber } }) + minetest.register_craft({ type="shaped", output="industrialtest:insulated_iron_cable 6", @@ -266,13 +371,26 @@ minetest.register_craft({ {industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber,industrialtest.elementKeys.rubber} } }) + industrialtest.api.registerCableFormerRecipe({ output="industrialtest:iron_cable 12", recipe="industrialtest:refined_iron_ingot", time=3 }) -registerCable("glass_fibre","Glass Fibre",0.15,industrialtest.api.ivPowerFlow,false) +industrialtest.GlassFibreCable=table.copy(industrialtest.Cable) +industrialtest.internal.unpackTableInto(industrialtest.GlassFibreCable,{ + name="industrialtest:glass_fibre_cable", + description=S("Glass Fibre Cable"), + inventoryImage="industrialtest_glass_fibre_cable_inv.png", + transparent=true, + tile="industrialtest_glass_fibre_cable.png", + size=0.12, + flow=industrialtest.api.ivPowerFlow +}) + +industrialtest.GlassFibreCable:register() + minetest.register_craft({ type="shaped", output="industrialtest:glass_fibre_cable 4",