Add digilines rail

This commit is contained in:
cheapie 2020-04-01 21:19:46 -05:00
parent 4dbe129a91
commit 28e7bab590
7 changed files with 211 additions and 0 deletions

12
README

@ -27,3 +27,15 @@ This mod adds several types of rails for use with the existing carts from minete
-- When not powered, causes any carts passing over it to come to an immediate stop.
-- When powered via mesecons while the cart is still stopped on it, causes the cart to resume moving with its previous speed.
-- If left powered, acts as a normal rail.
* Digilines Controlled Rail (available only if digilines is installed)
-- Sends a table on its set channel when passed over by a cart, including the following elements:
---- pos: Exact position of the cart
---- velocity: X and Z velocity of the cart
---- driver: Name of the player inside the cart (nil if none)
-- Responds to the following commands on its set channel:
---- "grab": Stops a cart traveling over the rail, same as the start-stop rail. Only does anything if a cart is present when the command is issued.
---- "release": Restarts a cart stopped via the "grab" command, causing it to resume moving with its previous speed.
---- "power1" through "power15": Causes the rail to act as a powered rail. The power of a normal powered rail is 5.
---- "brake1" through "brake15": Causes the rail to act as a brake rail. The power of a normal brake rail is 5.
---- "idle" (also "power0" or "brake0"): Causes the rail to act as a normal rail, applying no power or braking force.

198
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

@ -1,3 +1,4 @@
name = mesecons_carts
description = Mesecons Carts Integration
depends = carts,mesecons
optional_depends = digilines

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB