summaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorcheapie <no-email-for-you@example.com>2020-04-01 21:19:46 -0500
committercheapie <no-email-for-you@example.com>2020-04-01 21:19:46 -0500
commit28e7bab590448b17194f6923052595b56afcc4a9 (patch)
tree8b2f5080a5d6a6f278673de2977887fd3ffba702 /init.lua
parent4dbe129a91f3152b1ac1239e76737f3b00a8c673 (diff)
downloadmesecons_carts-28e7bab590448b17194f6923052595b56afcc4a9.tar
mesecons_carts-28e7bab590448b17194f6923052595b56afcc4a9.tar.gz
mesecons_carts-28e7bab590448b17194f6923052595b56afcc4a9.tar.bz2
mesecons_carts-28e7bab590448b17194f6923052595b56afcc4a9.tar.xz
mesecons_carts-28e7bab590448b17194f6923052595b56afcc4a9.zip
Add digilines rail
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua198
1 files changed, 198 insertions, 0 deletions
diff --git a/init.lua b/init.lua
index 8e2549e..336b50f 100644
--- a/init.lua
+++ b/init.lua
@@ -360,3 +360,201 @@ minetest.register_craft({
{"carts:rail","default:sand","",},
},
})
+
+if minetest.get_modpath("digilines") then
+ local digilines_rules = {
+ {x = 1,y = 0,z = 0,},
+ {x = -1,y = 0,z = 0,},
+ {x = 0,y = 0,z = 1,},
+ {x = 0,y = 0,z = -1,},
+ {x = 1,y = -1,z = 0,},
+ {x = -1,y = -1,z = 0,},
+ {x = 0,y = -1,z = 1,},
+ {x = 0,y = -1,z = -1,},
+ {x = 1,y = 1,z = 0,},
+ {x = -1,y = 1,z = 0,},
+ {x = 0,y = 1,z = 1,},
+ {x = 0,y = 1,z = -1,},
+ {x = 0,y = -1,z = 0,},
+ {x = 0,y = 1,z = 0,},
+ }
+
+ local function digilinesrail_onstep(cart,dtime)
+ local cartpos = cart.object:get_pos()
+ local trackpos = {}
+ trackpos.x = math.floor(cartpos.x + 0.5)
+ trackpos.y = math.floor(cartpos.y + 0.5)
+ trackpos.z = math.floor(cartpos.z + 0.5)
+ local track = minetest.get_node(trackpos)
+ if not string.find(track.name,"mesecons_carts:digilines_rail") then return end
+ local trackmeta = minetest.get_meta(trackpos)
+ local response = {}
+ local velocity = cart.object:get_velocity()
+ response.pos = {x = cartpos.x,y = cartpos.y,z = cartpos.z,}
+ response.velocity = {x = velocity.x,z = velocity.z,}
+ response.driver = cart.driver
+ digiline:receptor_send(trackpos,digilines_rules,trackmeta:get_string("channel"),response)
+ end
+
+ local function digilinesrail_handle_digilines(pos,node,channel,msg)
+ local trackmeta = minetest.get_meta(pos)
+ if channel ~= trackmeta:get_string("channel") then return end
+ if msg == "grab" then
+ local possible_carts = minetest.get_objects_inside_radius(pos,1)
+ for _,object in pairs(possible_carts) do
+ local cart = object:get_luaentity()
+ if cart and cart.name == "carts:cart" then
+ local velocity = cart.object:get_velocity()
+ trackmeta:set_string("velocity",minetest.serialize(velocity))
+ cart.object:set_velocity(vector.new(0,0,0))
+ cart.object:set_pos(pos)
+ end
+ end
+ elseif msg == "release" then
+ local velocity = trackmeta:get_string("velocity")
+ if velocity then velocity = minetest.deserialize(velocity) end
+ if not velocity then return end
+ local possible_carts = minetest.get_objects_inside_radius(pos,1)
+ for _,object in pairs(possible_carts) do
+ local cart = object:get_luaentity()
+ if cart and cart.name == "carts:cart" then
+ cart.object:set_velocity(velocity)
+ end
+ end
+ elseif msg == "idle" or msg == "power0" or msg == "brake0" then
+ minetest.swap_node(pos,{name = "mesecons_carts:digilines_rail_idle",})
+ elseif type(msg) == "string" then
+ if string.sub(msg,1,5) == "power" then
+ local strength = string.sub(msg,6,-1)
+ if strength then strength = tonumber(strength) end
+ if not strength then return end
+ strength = math.min(15,math.max(1,math.floor(strength)))
+ local newnode = "mesecons_carts:digilines_rail_pwr_"..strength
+ minetest.swap_node(pos,{name = newnode,})
+ elseif string.sub(msg,1,5) == "brake" then
+ local strength = string.sub(msg,6,-1)
+ if strength then strength = tonumber(strength) end
+ if not strength then return end
+ strength = math.min(15,math.max(1,math.floor(strength)))
+ local newnode = "mesecons_carts:digilines_rail_brk_"..strength
+ minetest.swap_node(pos,{name = newnode,})
+ end
+ end
+ end
+
+ carts:register_rail("mesecons_carts:digilines_rail_idle", {
+ description = "Digilines-Controlled Rail",
+ tiles = {
+ "mesecons_carts_digi_straight.png",
+ "mesecons_carts_digi_curve.png",
+ "mesecons_carts_digi_tjunction.png",
+ "mesecons_carts_digi_crossing.png",
+ },
+ after_place_node = function(pos)
+ local meta = minetest.get_meta(pos)
+ meta:set_string("formspec","field[channel;Channel;${channel}")
+ meta:set_string("channel","")
+ meta:set_string("velocity",minetest.serialize(vector.new(0,0,0)))
+ end,
+ on_receive_fields = function(pos, formname, fields, sender)
+ if not fields.channel then return end
+ local name = sender:get_player_name()
+ if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
+ minetest.record_protection_violation(pos,name)
+ return
+ end
+ minetest.get_meta(pos):set_string("channel",fields.channel)
+ end,
+ digiline = {
+ wire = {
+ rules = digilines_rules,
+ },
+ receptor = {},
+ effector = {
+ action = digilinesrail_handle_digilines,
+ },
+ },
+ groups = carts:get_rail_groups(),
+ }, {on_step = digilinesrail_onstep,})
+
+ for i=1,15,1 do
+ carts:register_rail("mesecons_carts:digilines_rail_pwr_"..i, {
+ description = string.format("Digilines-Controlled Rail (powered, strength %d - you hacker you!)",i),
+ tiles = {
+ "mesecons_carts_digi_straight.png",
+ "mesecons_carts_digi_curve.png",
+ "mesecons_carts_digi_tjunction.png",
+ "mesecons_carts_digi_crossing.png",
+ },
+ drop = "mesecons_carts:digilines_rail_idle",
+ after_place_node = function(pos)
+ local meta = minetest.get_meta(pos)
+ meta:set_string("formspec","field[channel;Channel;${channel}")
+ meta:set_string("channel","")
+ meta:set_string("velocity",minetest.serialize(vector.new(0,0,0)))
+ end,
+ on_receive_fields = function(pos, formname, fields, sender)
+ if not fields.channel then return end
+ local name = sender:get_player_name()
+ if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
+ minetest.record_protection_violation(pos,name)
+ return
+ end
+ minetest.get_meta(pos):set_string("channel",fields.channel)
+ end,
+ digiline = {
+ wire = {
+ rules = digilines_rules,
+ },
+ receptor = {},
+ effector = {
+ action = digilinesrail_handle_digilines,
+ },
+ },
+ groups = carts:get_rail_groups({not_in_creative_inventory = 1,}),
+ }, {on_step = digilinesrail_onstep,acceleration = i,})
+ carts:register_rail("mesecons_carts:digilines_rail_brk_"..i, {
+ description = string.format("Digilines-Controlled Rail (brake, strength %d - you hacker you!)",i),
+ tiles = {
+ "mesecons_carts_digi_straight.png",
+ "mesecons_carts_digi_curve.png",
+ "mesecons_carts_digi_tjunction.png",
+ "mesecons_carts_digi_crossing.png",
+ },
+ drop = "mesecons_carts:digilines_rail_idle",
+ after_place_node = function(pos)
+ local meta = minetest.get_meta(pos)
+ meta:set_string("formspec","field[channel;Channel;${channel}")
+ meta:set_string("channel","")
+ meta:set_string("velocity",minetest.serialize(vector.new(0,0,0)))
+ end,
+ on_receive_fields = function(pos, formname, fields, sender)
+ if not fields.channel then return end
+ local name = sender:get_player_name()
+ if minetest.is_protected(pos,name) and not minetest.check_player_privs(name,{protection_bypass=true}) then
+ minetest.record_protection_violation(pos,name)
+ return
+ end
+ minetest.get_meta(pos):set_string("channel",fields.channel)
+ end,
+ digiline = {
+ wire = {
+ rules = digilines_rules,
+ },
+ receptor = {},
+ effector = {
+ action = digilinesrail_handle_digilines,
+ },
+ },
+ groups = carts:get_rail_groups({not_in_creative_inventory = 1,}),
+ }, {on_step = digilinesrail_onstep,acceleration = i*-1,})
+ end
+ minetest.register_craft({
+ output = "mesecons_carts:digilines_rail_idle 3",
+ recipe = {
+ {"carts:rail","basic_materials:motor","",},
+ {"carts:rail","mesecons_luacontroller:luacontroller0000","digilines:wire_std_00000000",},
+ {"carts:rail","default:sand","",},
+ },
+ })
+end