distributor: delayed activation improvement

ball: vanishes properly when objects deactivates
master
rnd1 2016-12-25 12:52:29 +01:00
parent 24e66ea382
commit 55eafde324
2 changed files with 60 additions and 59 deletions

View File

@ -86,6 +86,7 @@ minetest.register_entity("basic_machines:ball",{
speed = 5, -- velocity when punched
hurt = 0, -- how much damage it does to target entity, if 0 damage disabled
owner = "",
state = false,
origin = {x=0,y=0,z=0},
lastpos = {x=0,y=0,z=0}, -- last not-colliding position
hp_max = 100,
@ -105,6 +106,13 @@ minetest.register_entity("basic_machines:ball",{
self.lifetime = 20;
end,
get_staticdata = function(self) -- this gets called before object put in world and before it hides
if not self.state then return nil end
self.object:remove();
return nil
end,
on_punch = function (self, puncher, time_from_last_punch, tool_capabilities, dir)
if self.puncheable == 0 then return end
if self.puncheable == 1 then -- only those in protection
@ -129,6 +137,7 @@ minetest.register_entity("basic_machines:ball",{
return
end
if not self.state then self.state = true end
local pos=self.object:getpos()
local origin = self.origin;

110
mover.lua
View File

@ -1383,44 +1383,42 @@ minetest.register_node("basic_machines:distributor", {
return
end
local posf = {}; local active = {};
local n = meta:get_int("n");local delay = meta:get_float("delay");
for i =1,n do
posf[i]={x=meta:get_int("x"..i)+pos.x,y=meta:get_int("y"..i)+pos.y,z=meta:get_int("z"..i)+pos.z};
active[i]=meta:get_int("active"..i);
end
local table,node;
local delay = minetest.get_meta(pos):get_float("delay");
for i=1,n do
if active[i]~=0 then
node = minetest.get_node(posf[i]);if not node.name then return end -- error
table = minetest.registered_nodes[node.name];
if table and table.mesecons and table.mesecons.effector then -- check if all elements exist, safe cause it checks from left to right
-- alternative way: overkill
--ret = pcall(function() if not table.mesecons.effector then end end); -- exception handling to determine if structure exists
local effector=table.mesecons.effector;
local activate = function()
local posf = {}; local active = {};
local n = meta:get_int("n");local delay = meta:get_float("delay");
for i =1,n do
posf[i]={x=meta:get_int("x"..i)+pos.x,y=meta:get_int("y"..i)+pos.y,z=meta:get_int("z"..i)+pos.z};
active[i]=meta:get_int("active"..i);
end
local table,node;
for i=1,n do
if active[i]~=0 then
node = minetest.get_node(posf[i]);if not node.name then return end -- error
table = minetest.registered_nodes[node.name];
if (active[i] == 1 or active[i] == 2) and effector.action_on then -- normal OR only forward input ON
if delay>0 then
minetest.after(delay, function() effector.action_on(posf[i],node,ttl-1) end);
else
effector.action_on(posf[i],node,ttl-1);
end
elseif active[i] == -1 and effector.action_off then
if delay>0 then
minetest.after(delay, function() effector.action_off(posf[i],node,ttl-1) end);
else
if table and table.mesecons and table.mesecons.effector then -- check if all elements exist, safe cause it checks from left to right
-- alternative way: overkill
--ret = pcall(function() if not table.mesecons.effector then end end); -- exception handling to determine if structure exists
local effector=table.mesecons.effector;
if (active[i] == 1 or active[i] == 2) and effector.action_on then -- normal OR only forward input ON
effector.action_on(posf[i],node,ttl-1);
elseif active[i] == -1 and effector.action_off then
effector.action_off(posf[i],node,ttl-1)
end
end
end
end
end
if delay>0 then minetest.after(delay, activate) else activate() end
end,
action_off = function (pos, node,ttl)
@ -1447,37 +1445,28 @@ minetest.register_node("basic_machines:distributor", {
meta:set_string("infotext","overheat: temperature ".. T)
return
end
local posf = {}; local active = {};
local n = meta:get_int("n");
for i =1,n do
posf[i]={x=meta:get_int("x"..i)+pos.x,y=meta:get_int("y"..i)+pos.y,z=meta:get_int("z"..i)+pos.z};
active[i]=meta:get_int("active"..i);
end
local node, table
local delay = minetest.get_meta(pos):get_float("delay");
for i=1,n do
if active[i]~=0 then
node = minetest.get_node(posf[i]);if not node.name then return end -- error
table = minetest.registered_nodes[node.name];
if table and table.mesecons and table.mesecons.effector then
local effector=table.mesecons.effector;
if (active[i] == 1 or active[i]==-2) and effector.action_off then -- normal OR only forward input OFF
if delay>0 then
minetest.after(delay, function() effector.action_off(posf[i],node,ttl-1) end);
else
local activate = function()
local posf = {}; local active = {};
local n = meta:get_int("n");
for i =1,n do
posf[i]={x=meta:get_int("x"..i)+pos.x,y=meta:get_int("y"..i)+pos.y,z=meta:get_int("z"..i)+pos.z};
active[i]=meta:get_int("active"..i);
end
local node, table
for i=1,n do
if active[i]~=0 then
node = minetest.get_node(posf[i]);if not node.name then return end -- error
table = minetest.registered_nodes[node.name];
if table and table.mesecons and table.mesecons.effector then
local effector=table.mesecons.effector;
if (active[i] == 1 or active[i]==-2) and effector.action_off then -- normal OR only forward input OFF
effector.action_off(posf[i],node,ttl-1);
end
elseif (active[i] == -1) and effector.action_on then
if delay>0 then
minetest.after(delay, function() effector.action_on(posf[i],node,ttl-1) end);
else
elseif (active[i] == -1) and effector.action_on then
effector.action_on(posf[i],node,ttl-1);
end
end
@ -1485,6 +1474,8 @@ minetest.register_node("basic_machines:distributor", {
end
end
if delay>0 then minetest.after(delay, activate) else activate() end
end
}
},
@ -2310,8 +2301,9 @@ minetest.register_on_player_receive_fields(function(player,formname,fields)
"You can add more targets with ADD. To see where target node is click SHOW button next to it.\n"..
"Numbers in each row represent (from left to right) : first 3 numbers are target coordinates,\n"..
"last number controls how signal is passed to target. For example, to only pass OFF signal use -2,\n"..
"to only pass ON use 2, -1 negates the signal, 1 = pass original signal, 0 blocks signal\n\n"..
"ADVANCED: you can use distributor as a even handler. First you must deactivate first target by putting 0 at\n"..
"to only pass ON use 2, -1 negates the signal, 1 = pass original signal, 0 blocks signal\n"..
"delay option adds delay to activations, in seconds\n\n"..
"ADVANCED: you can use distributor as an event handler. First you must deactivate first target by putting 0 at\n"..
"last place in first line. Meanings of first 2 numbers are as follows: first number 0/1 controls if node/n".. "listens to failed interact attempts around it, second number -1/1 listens to chat and can mute it";
local form = "size [5.5,5.5] textarea[0,0;6,7;help;DISTRIBUTOR HELP;".. text.."]"
minetest.show_formspec(name, "basic_machines:help_distributor", form)