diff options
Diffstat (limited to 'trafficlight')
-rw-r--r-- | trafficlight/init.lua | 322 | ||||
-rw-r--r-- | trafficlight/old2new.lua | 58 |
2 files changed, 240 insertions, 140 deletions
diff --git a/trafficlight/init.lua b/trafficlight/init.lua index eaa0a79..562068f 100644 --- a/trafficlight/init.lua +++ b/trafficlight/init.lua @@ -1,44 +1,96 @@ --[[ StreetsMod: inDev Trafficlights ]] -minetest.register_node(":streets:trafficlight_bottom",{ - description = S("Cheater"), - drop = "", - groups = {not_in_creative_inventory = 1}, - paramtype = "light", - paramtype2 = "facedir", - drawtype = "nodebox", - sunlight_propagates = true, - tiles = {"streets_pole.png"}, - node_box = { - type = "fixed", - fixed = { - {-0.15,-0.5,-0.15,0.15,0.25,0.15}, - {-0.1,0.25,-0.1,0.1,0.5,0.1} - } - }, -}) +dofile(streets.modpath .. "/../trafficlight/old2new.lua") -minetest.register_node(":streets:trafficlight_middle",{ - description = S("Cheater!"), - drop = "", - groups = {cracky = 1, not_in_creative_inventory = 1}, - paramtype = "light", - drawtype = "nodebox", - sunlight_propagates = true, - tiles = {"streets_pole.png"}, - node_box = { - type = "fixed", - fixed = { - {-0.1,-0.5,-0.1,0.1,0.5,0.1}, - } +streets.tlBox = { + --[[ Thank you, rubenwardy, for your awesome NodeboxEditor! Not perfect, but still great! ]] + {-0.1875,-0.5,0.5,0.1875,0.5,0.75}, --nodebox1 + {-0.0625,0.375,0.3125,0.0625,0.4375,0.5}, --nodebox2 + {-0.0625,0.0625,0.3125,0.0625,0.125,0.5}, --nodebox3 + {-0.0625,-0.25,0.3125,0.0625,-0.1875,0.5}, --nodebox4 + {0.0625,0.3125,0.3125,0.125,0.38,0.5}, --nodebox5 + {-0.125,0.3125,0.3125,-0.0625,0.375,0.5}, --nodebox6 + {0.0625,0,0.3125,0.125,0.0625,0.5}, --nodebox7 + {-0.125,0,0.3125,-0.0625,0.0625,0.5}, --nodebox8 + {0.0625,-0.3125,0.3125,0.125,-0.25,0.5}, --nodebox9 + {-0.125,-0.3125,0.3125,-0.0625,-0.25,0.5}, --nodebox10 + {-0.125, -0.125, 0.85, 0.125, 0.125, 0.75}, -- NodeBox11 +} + +streets.tlRythm = { + toRed = { + {name = "streets:trafficlight_top_yellow", pauseBefore = 0}, + {name = "streets:trafficlight_top_red", pauseBefore = 3} }, - pointable = false, -}) + toGreen = { + {name = "streets:trafficlight_top_redyellow", pauseBefore = 0}, + {name = "streets:trafficlight_top_green", pauseBefore = 1.5} + }, + toOff = { + {name = "streets:trafficlight_top_warn", pauseBefore = 0}, + {name = "streets:trafficlight_top_off", pauseBefore = 5} + }, + toWarn = { + {name = "streets:trafficlight_top_warn", pauseBefore = 0} + } +} -minetest.register_node(":streets:trafficlight_controller",{ - description = S("Trafficlight"), - inventory_image = "streets_trafficlight_inv.png", +streets.tlSwitch = function(def) + if not def.pos or not def.to or not streets.tlRythm[def.to] then + return + end + local meta = minetest.get_meta(def.pos) + -- Only switch if new state ~= current state + if "to" .. meta:get_string("state") == def.to then + return + end + -- Switch the trafficlight + for k, v in pairs(streets.tlRythm[def.to]) do + minetest.get_meta(def.pos):set_string("state", def.to:gsub("to", "")) + minetest.after(v.pauseBefore, function() + minetest.swap_node(def.pos, {name = v.name, param2 = minetest.get_node(def.pos).param2}) + end) + end +end + +streets.on_digiline_receive = function(pos, node, channel, msg) + local setchan = minetest.get_meta(pos):get_string("channel") + if setchan ~= channel then + return + end + -- Tl states + if msg == "OFF" then + streets.tlSwitch({ + pos = pos, + to = "toOff" + }) + elseif msg == "GREEN" then + streets.tlSwitch({ + pos = pos, + to = "toGreen" + }) + elseif msg == "RED" then + streets.tlSwitch({ + pos = pos, + to = "toRed" + }) + elseif msg == "WARN" then + streets.tlSwitch({ + pos = pos, + to = "toWarn" + }) + elseif msg == "GET" then + local state = minetest.get_meta(pos):get_string("state") + if not state or state == "" then + state = "UNDEFINED" + end + digiline:receptor_send(pos, digiline.rules.default, channel, state) + end +end + +minetest.register_node(":streets:digiline_distributor",{ + description = streets.S("Digiline distributor"), tiles = {"streets_lampcontroller_top.png","streets_lampcontroller_bottom.png","streets_lampcontroller_sides.png"}, groups = {cracky = 1}, drawtype = "nodebox", @@ -50,95 +102,55 @@ minetest.register_node(":streets:trafficlight_controller",{ {-0.05,0.5,-0.05,0.05,1.6,0.05} } }, - selection_box = { - type = "fixed", - fixed = { - {-0.5,-0.5,-0.5,0.5,0.5,0.5}, - {-0.3,1.5,-0.3,0.3,4.5,0.3} - } - }, - on_receive_fields = function(pos, formname, fields, sender) - minetest.get_meta(pos):set_string("channel", fields.channel) - end, - after_place_node = function(pos,placer,itemstack) - minetest.set_node({x = pos.x, y = pos.y - 2, z = pos.z},{name = "streets:trafficlight_controller"}) - minetest.set_node({x = pos.x, y = pos.y + 0, z = pos.z},{name = "streets:trafficlight_bottom"}) - minetest.set_node({x = pos.x, y = pos.y + 1, z = pos.z},{name = "streets:trafficlight_middle"}) - minetest.set_node({x = pos.x, y = pos.y + 2, z = pos.z},{name = "streets:trafficlight_top_off",param2 = minetest.dir_to_facedir(placer:get_look_dir())}) - local meta = minetest.get_meta({x = pos.x, y = pos.y - 2, z = pos.z}) - meta:set_string("channel","") - meta:set_string("infotext",S("Off")) - meta:set_string("formspec","field[channel;"..S("Channel")..";${channel}]") - end, - after_dig_node = function(pos) - minetest.remove_node({x = pos.x, y = pos.y + 2, z = pos.z}) - minetest.remove_node({x = pos.x, y = pos.y + 3, z = pos.z}) - minetest.remove_node({x = pos.x, y = pos.y + 4, z = pos.z}) - end, digiline = { - receptor = {}, - effector = { - action = function(pos,node,channel,msg) - local setchannel = minetest.get_meta(pos):get_string("channel") - if channel == setchannel then - -- Set a meta entry for the trafficlight's state - local meta = minetest.get_meta(pos) - local state = meta:get_string("infotext") - if msg == "green" or msg == "red" or msg == "warn" or msg == "off" then - meta:set_string("infotext",S(msg)) - local facedir = minetest.get_node({x = pos.x, y = pos.y + 4, z = pos.z}).param2 - -- Modify <pos> to the top node of the trafficlight - pos.y = pos.y + 4 - -- - if msg == "red" and state ~= "red" then - minetest.set_node(pos,{name = "streets:trafficlight_top_yellow",param2=facedir}) - minetest.after(3,function(param) - minetest.set_node(pos,{name = "streets:trafficlight_top_red",param2=facedir}) - end) - end - -- - if msg == "green" and state ~= "green" then - minetest.set_node(pos,{name = "streets:trafficlight_top_redyellow",param2=facedir}) - minetest.after(3,function(param) - minetest.set_node(pos,{name = "streets:trafficlight_top_green",param2=facedir}) - end) - end - -- - if msg == "off" and state ~= "off" then - minetest.set_node(pos,{name = "streets:trafficlight_top_off",param2=facedir}) - end - -- - if msg == "warn" and state ~= "warn" then - minetest.set_node(pos,{name = "streets:trafficlight_top_warn",param2=facedir}) - end - end - end - end + wire = { + rules = { + {x= 0, y= 0, z=-1}, + {x= 0, y= 0, z= 1}, + {x= 1, y= 0, z= 0}, + {x=-1, y= 0, z= 0}, + {x= 0, y= 2, z=0} + } } } }) minetest.register_node(":streets:trafficlight_top_off",{ - description = S("U cheater U"), - drop = "", - groups = {cracky = 1, not_in_creative_inventory = 1}, + description = streets.S("Trafficlight"), + drawtype="nodebox", paramtype = "light", paramtype2 = "facedir", + groups = {cracky = 1, level = 2}, + inventory_image = "streets_trafficlight_inv.png", + light_source = 11, sunlight_propagates = true, - drawtype = "nodebox", - tiles = {"streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_off.png"}, node_box = { type = "fixed", - fixed = { - {-0.25,-0.5,-0.25,0.25,0.5,0.25} + fixed = streets.tlBox + }, + tiles = {"streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_off.png"}, + digiline = { + receptor = {}, + effector = { + action = function(pos, node, channel, msg) + streets.on_digiline_receive(pos, node, channel, msg) + end } }, - pointable = false, + on_construct = function(pos) + local meta = minetest.get_meta(pos) + meta:set_string("formspec", "field[channel;Channel;${channel}]") + end, + on_receive_fields = function(pos, formname, fields, sender) + if (fields.channel) then + minetest.get_meta(pos):set_string("channel", fields.channel) + minetest.get_meta(pos):set_string("state", "Off") + end + end, }) minetest.register_node(":streets:trafficlight_top_red",{ - description = S("U cheater U"), - drop = "", + drop = "streets:trafficlight_top_off", groups = {cracky = 1, not_in_creative_inventory = 1}, paramtype = "light", paramtype2 = "facedir", @@ -147,17 +159,21 @@ minetest.register_node(":streets:trafficlight_top_red",{ tiles = {"streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_red.png"}, node_box = { type = "fixed", - fixed = { - {-0.25,-0.5,-0.25,0.25,0.5,0.25} - } + fixed = streets.tlBox }, - pointable = false, light_source = 6, + digiline = { + receptor = {}, + effector = { + action = function(pos, node, channel, msg) + streets.on_digiline_receive(pos, node, channel, msg) + end + } + }, }) minetest.register_node(":streets:trafficlight_top_yellow",{ - description = S("U cheater U"), - drop = "", + drop = "streets:trafficlight_top_off", groups = {cracky = 1, not_in_creative_inventory = 1}, paramtype = "light", paramtype2 = "facedir", @@ -166,17 +182,21 @@ minetest.register_node(":streets:trafficlight_top_yellow",{ tiles = {"streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_yellow.png"}, node_box = { type = "fixed", - fixed = { - {-0.25,-0.5,-0.25,0.25,0.5,0.25} - } + fixed = streets.tlBox }, - pointable = false, light_source = 6, + digiline = { + receptor = {}, + effector = { + action = function(pos, node, channel, msg) + streets.on_digiline_receive(pos, node, channel, msg) + end + } + }, }) minetest.register_node(":streets:trafficlight_top_redyellow",{ - description = S("U cheater U"), - drop = "", + drop = "streets:trafficlight_top_off", groups = {cracky = 1, not_in_creative_inventory = 1}, paramtype = "light", paramtype2 = "facedir", @@ -185,17 +205,21 @@ minetest.register_node(":streets:trafficlight_top_redyellow",{ tiles = {"streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_redyellow.png"}, node_box = { type = "fixed", - fixed = { - {-0.25,-0.5,-0.25,0.25,0.5,0.25} - } + fixed = streets.tlBox }, - pointable = false, light_source = 6, + digiline = { + receptor = {}, + effector = { + action = function(pos, node, channel, msg) + streets.on_digiline_receive(pos, node, channel, msg) + end + } + }, }) minetest.register_node(":streets:trafficlight_top_green",{ - description = S("U cheater U"), - drop = "", + drop = "streets:trafficlight_top_off", groups = {cracky = 1, not_in_creative_inventory = 1}, paramtype = "light", paramtype2 = "facedir", @@ -204,17 +228,21 @@ minetest.register_node(":streets:trafficlight_top_green",{ tiles = {"streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_bg.png","streets_tl_green.png"}, node_box = { type = "fixed", - fixed = { - {-0.25,-0.5,-0.25,0.25,0.5,0.25} - } + fixed = streets.tlBox }, - pointable = false, light_source = 6, + digiline = { + receptor = {}, + effector = { + action = function(pos, node, channel, msg) + streets.on_digiline_receive(pos, node, channel, msg) + end + } + }, }) minetest.register_node(":streets:trafficlight_top_warn",{ - description = S("U cheater U"), - drop = "", + drop = "streets:trafficlight_top_off", groups = {cracky = 1, not_in_creative_inventory = 1}, paramtype = "light", paramtype2 = "facedir", @@ -226,19 +254,33 @@ minetest.register_node(":streets:trafficlight_top_warn",{ }}, node_box = { type = "fixed", - fixed = { - {-0.25,-0.5,-0.25,0.25,0.5,0.25} - } + fixed = streets.tlBox }, - pointable = false, light_source = 6, + digiline = { + receptor = {}, + effector = { + action = function(pos, node, channel, msg) + streets.on_digiline_receive(pos, node, channel, msg) + end + } + }, }) minetest.register_craft({ - output = "streets:trafficlight_controller", + output = "streets:trafficlight_top_off", recipe = { - {"dye:red","",""}, - {"dye:yellow","",""}, - {"dye:green","",""} + {"default:steel_ingot", "dye:red", "default:steel_ingot"}, + {"default:steel_ingot", "dye:yellow", "default:steel_ingot"}, + {"default:steel_ingot", "dye:green", "default:steel_ingot"} } }) + +minetest.register_craft({ + output = "streets:digiline_distributor", + recipe = { + {"", "digilines:wire_std_00000000", ""}, + {"digilines:wire_std_00000000", "mesecons_luacontroller:luacontroller0000", "digilines:wire_std_00000000"}, + {"", "digilines:wire_std_00000000", ""} + } +})
\ No newline at end of file diff --git a/trafficlight/old2new.lua b/trafficlight/old2new.lua new file mode 100644 index 0000000..51ba9f3 --- /dev/null +++ b/trafficlight/old2new.lua @@ -0,0 +1,58 @@ +--[[ + StreetsMod: Convert old trafficlights +]] +minetest.register_node(":streets:trafficlight_bottom", { + diggable = false, + pointable = false, + drawtype = "airlike", + description = "I'm an old node, please drop me", + groups = {not_in_creative_inventory = 1} +}) +minetest.register_abm({ + nodenames = {"streets:trafficlight_bottom"}, + interval = 1, + chance = 1, + action = function(pos, node) + minetest.log("action", "Converting trafficlight at position " .. minetest.pos_to_string(pos)) + -- Replace controller with distributor + pos.y = pos.y - 2 + minetest.set_node(pos, {name = "streets:digiline_distributor"}) + -- Change bottom pole + pos.y = pos.y + 2 + minetest.set_node(pos, {name = "streets:bigpole", param2 = 2}) + -- Change middle pole + pos.y = pos.y + 1 + minetest.set_node(pos, {name = "streets:bigpole", param2 = 2}) + -- Change the top + pos.y = pos.y + 1 + local fd = minetest.get_node(pos).param2 + local ch = minetest.get_meta(pos):get_string("channel") + minetest.set_node(pos, {name = "streets:bigpole", param2 = 2}) + -- Place new top + if fd == 1 then + minetest.set_node({x = pos.x - 1, y = pos.y, z = pos.z}, {name = "streets:trafficlight_top_warn", param2 = fd}) + local meta = minetest.get_meta({x = pos.x - 1, y = pos.y, z = pos.z}) + meta:set_string("channel", ch) + meta:set_string("state", "warn") + meta:set_string("formspec", "field[channel;Channel;${channel}]") + elseif fd == 2 then + minetest.set_node({x = pos.x, y = pos.y, z = pos.z + 1}, {name = "streets:trafficlight_top_warn", param2 = fd}) + local meta = minetest.get_meta({x = pos.x, y = pos.y, z = pos.z + 1}) + meta:set_string("channel", ch) + meta:set_string("state", "warn") + meta:set_string("formspec", "field[channel;Channel;${channel}]") + elseif fd == 3 then + minetest.set_node({x = pos.x + 1, y = pos.y, z = pos.z}, {name = "streets:trafficlight_top_warn", param2 = fd}) + local meta = minetest.get_meta({x = pos.x + 1, y = pos.y, z = pos.z}) + meta:set_string("channel", ch) + meta:set_string("state", "warn") + meta:set_string("formspec", "field[channel;Channel;${channel}]") + elseif fd == 0 then + minetest.set_node({x = pos.x, y = pos.y, z = pos.z - 1}, {name = "streets:trafficlight_top_warn", param2 = fd}) + local meta = minetest.get_meta({x = pos.x, y = pos.y, z = pos.z - 1}) + meta:set_string("channel", ch) + meta:set_string("state", "warn") + meta:set_string("formspec", "field[channel;Channel;${channel}]") + end + end +})
\ No newline at end of file |