Support for the mesecons mvps callback

This commit is contained in:
Novatux 2014-01-03 13:29:38 +01:00
parent 9a9bd2f491
commit d33eb89dcb
4 changed files with 120 additions and 65 deletions

View File

@ -157,3 +157,12 @@ minetest.register_on_dignode(function(pos, oldnode, digger)
end end
end) end)
if minetest.get_modpath("mesecons_mvps") ~= nil then
mesecon:register_on_mvps_move(function(moved_nodes)
for _, n in ipairs(moved_nodes) do
pipeworks.scan_for_tube_objects(n.pos)
pipeworks.scan_for_tube_objects(n.oldpos)
end
end)
end

View File

@ -1,3 +1,3 @@
default default
mesecons? mesecons?
mesecons_mvps?

View File

@ -467,3 +467,26 @@ minetest.register_entity("pipeworks:tubed_item", {
end end
}) })
if minetest.get_modpath("mesecons_mvps") ~= nil then
mesecon:register_mvps_unmov("pipeworks:tubed_item")
mesecon:register_on_mvps_move(function(moved_nodes)
local objects_to_move = {}
for _, n in ipairs(moved_nodes) do
local objects = minetest.get_objects_inside_radius(n.oldpos, 1)
for _, obj in ipairs(objects) do
local entity = obj:get_luaentity()
if entity and entity.name == "pipeworks:tubed_item" then
objects_to_move[#objects_to_move+1] = obj
end
end
end
if #objects_to_move > 0 then
local dir = vector.subtract(moved_nodes[1].pos, moved_nodes[1].oldpos)
for _, obj in ipairs(objects_to_move) do
local entity = obj:get_luaentity()
obj:setpos(vector.add(obj:getpos(), dir))
entity.start_pos = vector.add(entity.start_pos, dir)
end
end
end)
end

View File

@ -16,6 +16,18 @@ local function write_file(tbl)
f:close() f:close()
end end
local function update_pos_in_file(pos)
local tbl=read_file()
for _,val in ipairs(tbl) do
if val.x==pos.x and val.y==pos.y and val.z==pos.z then
local meta = minetest.get_meta(val)
val.channel = meta:get_string("channel")
val.cr = meta:get_int("can_receive")
end
end
write_file(tbl)
end
local function add_tube_in_file(pos,channel, cr) local function add_tube_in_file(pos,channel, cr)
local tbl=read_file() local tbl=read_file()
for _,val in ipairs(tbl) do for _,val in ipairs(tbl) do
@ -45,6 +57,7 @@ local function get_tubes_in_file(pos,channel)
for _,val in ipairs(tbl) do for _,val in ipairs(tbl) do
local node = minetest.get_node(val) local node = minetest.get_node(val)
local meta = minetest.get_meta(val) local meta = minetest.get_meta(val)
-- That shouldn't be needed anymore since the mvps callback, but we leave it nevertheless
if node.name~="ignore" and (val.channel~=meta:get_string("channel") or val.cr~=meta:get_int("can_receive")) then if node.name~="ignore" and (val.channel~=meta:get_string("channel") or val.cr~=meta:get_int("can_receive")) then
val.channel=meta:get_string("channel") val.channel=meta:get_string("channel")
val.cr=meta:get_int("can_receive") val.cr=meta:get_int("can_receive")
@ -68,72 +81,82 @@ local teleport_short_texture="pipeworks_teleport_tube_short.png"
local teleport_inv_texture="pipeworks_teleport_tube_inv.png" local teleport_inv_texture="pipeworks_teleport_tube_inv.png"
pipeworks.register_tube("pipeworks:teleport_tube","Teleporter pneumatic tube segment",teleport_plain_textures, pipeworks.register_tube("pipeworks:teleport_tube","Teleporter pneumatic tube segment",teleport_plain_textures,
teleport_noctr_textures,teleport_end_textures,teleport_short_texture,teleport_inv_texture, { teleport_noctr_textures,teleport_end_textures,teleport_short_texture,teleport_inv_texture, {
tube = { tube = {
can_go = function(pos,node,velocity,stack) can_go = function(pos,node,velocity,stack)
velocity.x = 0 velocity.x = 0
velocity.y = 0 velocity.y = 0
velocity.z = 0 velocity.z = 0
local meta = minetest.get_meta(pos)
local channel = meta:get_string("channel")
local target = get_tubes_in_file(pos,channel)
if target[1] == nil then return {} end
local d = math.random(1,#target)
pos.x = target[d].x
pos.y = target[d].y
pos.z = target[d].z
return pipeworks.meseadjlist
end
},
on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("channel","") local channel = meta:get_string("channel")
meta:set_int("can_receive",1) local target = get_tubes_in_file(pos,channel)
meta:set_string("formspec","size[9,1;]".. if target[1] == nil then return {} end
local d = math.random(1,#target)
pos.x = target[d].x
pos.y = target[d].y
pos.z = target[d].z
return pipeworks.meseadjlist
end
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("channel","")
meta:set_int("can_receive",1)
meta:set_string("formspec","size[9,1;]"..
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
"button[8,0;1,1;bt;On]")
add_tube_in_file(pos,"")
end,
on_receive_fields = function(pos,formname,fields,sender)
local meta = minetest.get_meta(pos)
--check for private channels
if fields.channel ~= nil then
local name, mode = fields.channel:match("^([^:;]+)([:;])")
if name and mode and name ~= sender:get_player_name() then
--channels starting with '[name]:' can only be used by the named player
if mode == ":" then
minetest.chat_send_player(sender:get_player_name(), "Sorry, channel '"..fields.channel.."' is reserved for exclusive use by "..name)
return
--channels starting with '[name];' can be used by other players, but cannot be received from
elseif mode == ";" and (meta:get_int("can_receive") ~= 0) == (fields["bt"] == nil) then
minetest.chat_send_player(sender:get_player_name(), "Sorry, receiving from channel '"..fields.channel.."' is reserved for "..name)
return
end
end
end
if fields.channel==nil then fields.channel=meta:get_string("channel") end
meta:set_string("channel",fields.channel)
remove_tube_in_file(pos)
local cr = meta:get_int("can_receive")
if fields["bt"] then
cr=1-cr
meta:set_int("can_receive",cr)
if cr==1 then
meta:set_string("formspec","size[9,1;]"..
"field[0,0.5;7,1;channel;Channel:;${channel}]".. "field[0,0.5;7,1;channel;Channel:;${channel}]"..
"button[8,0;1,1;bt;On]") "button[8,0;1,1;bt;On]")
add_tube_in_file(pos,"") else
end, meta:set_string("formspec","size[9,1;]"..
on_receive_fields = function(pos,formname,fields,sender) "field[0,0.5;7,1;channel;Channel:;${channel}]"..
local meta = minetest.get_meta(pos) "button[8,0;1,1;bt;Off]")
--check for private channels
if fields.channel ~= nil then
local name, mode = fields.channel:match("^([^:;]+)([:;])")
if name and mode and name ~= sender:get_player_name() then
--channels starting with '[name]:' can only be used by the named player
if mode == ":" then
minetest.chat_send_player(sender:get_player_name(), "Sorry, channel '"..fields.channel.."' is reserved for exclusive use by "..name)
return
--channels starting with '[name];' can be used by other players, but cannot be received from
elseif mode == ";" and (meta:get_int("can_receive") ~= 0) == (fields["bt"] == nil) then
minetest.chat_send_player(sender:get_player_name(), "Sorry, receiving from channel '"..fields.channel.."' is reserved for "..name)
return
end
end
end end
end
if fields.channel==nil then fields.channel=meta:get_string("channel") end add_tube_in_file(pos,fields.channel, cr)
meta:set_string("channel",fields.channel) end,
remove_tube_in_file(pos) on_destruct = function(pos)
local cr = meta:get_int("can_receive") remove_tube_in_file(pos)
if fields["bt"] then end})
cr=1-cr
meta:set_int("can_receive",cr) if minetest.get_modpath("mesecons_mvps") ~= nil then
if cr==1 then mesecon:register_on_mvps_move(function(moved_nodes)
meta:set_string("formspec","size[9,1;]".. for _, n in ipairs(moved_nodes) do
"field[0,0.5;7,1;channel;Channel:;${channel}]".. if string.find(n.node.name, "pipeworks:teleport_tube") ~= nil then
"button[8,0;1,1;bt;On]") update_pos_in_file(n.pos)
else
meta:set_string("formspec","size[9,1;]"..
"field[0,0.5;7,1;channel;Channel:;${channel}]"..
"button[8,0;1,1;bt;Off]")
end
end end
add_tube_in_file(pos,fields.channel, cr) end
end, end)
on_destruct = function(pos) end
remove_tube_in_file(pos)
end})