Coding-help needed for "walk-over - Node"

User avatar
Echo
Member
 
Posts: 121
Joined: Tue Jul 31, 2012 08:11

Coding-help needed for "walk-over - Node"

by Echo » Wed Aug 29, 2012 13:28

Hi,

I tried to code a node, when a player walks on it, it activates. When the player leaves the node, the action is performed.

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_abm(
    {nodenames = {"maze:closer"},
    interval = 1,
    chance = 1,
    action = function(pos, node, active_object_count, active_object_count_wider)
        local objs = minetest.env:get_objects_inside_radius(pos, 3)
        for k, obj in pairs(objs) do
            local objpos = obj:getpos()
            local dist = math.sqrt( ((pos.x - objpos.x) * (pos.x - objpos.x)) +  ((pos.y - (objpos.y - 0.5)) * (pos.y - (objpos.y - 0.5))) +  ((pos.z - objpos.z) * (pos.z - objpos.z)) )
            print (dist.." for "..minetest.pos_to_string(pos).." to player "..minetest.pos_to_string(objpos))
            local meta = minetest.env:get_meta(pos)
            if dist < 0.7 then -- player walked on node
                meta:set_string("trap", "triggered")
            elseif dist > 2.1 then -- at least one node away
                if meta:get_string("trap") == "triggered" then
                    meta:set_string("trap", "")
                    minetest.env:add_node(pos,{name="default:cobble"})
                    minetest.env:add_node({x = pos.x, y = pos.y + 1, z = pos.z},{name="default:cobble"})
                    minetest.env:add_node({x = pos.x, y = pos.y + 2, z = pos.z},{name="default:cobble"})
                end
            end
        end   
    end,
})


What it does:
A player walks over a node (distance < 0.7 -- the diagonal from middle of node to edge) then a certain string is written in the meta-data of the node.
When a player is near that node (distance > 2.1) and the certain string is in the meta-data, some action is performed.

Problem:
The abm isn't called often enough. When the player "runs" over the node, the distance is mostly bigger than 1. When walking "slowly" everythings fine. But I want the node to be triggered in any case.
If I increase the trigger-distance, the node is triggered, even when the player isn't walking over the node but passes it.

Any ideas?
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Wed Aug 29, 2012 13:41

Write all positions of the nodes in a table and then use register_globalstep().
Last edited by PilzAdam on Wed Aug 29, 2012 13:42, edited 1 time in total.
 

User avatar
Echo
Member
 
Posts: 121
Joined: Tue Jul 31, 2012 08:11

by Echo » Wed Aug 29, 2012 13:50

I wanted to avoid globalstep, but when an experienced coder like you proposes this, it's probably the best way.

Thank you very much!

Edit:
For everybody with the same problem (traps), my solution:
In register_abm a list of my nodes is created.
In register_globalstep the list is checked against all players. If a node is close enough to a player, it is triggered, if the distance is far enough, a triggered node is fired. No more problem with too big steps.
Last edited by Echo on Wed Aug 29, 2012 17:39, edited 1 time in total.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 21 guests