Page 1 of 1

Simulate player dig/place

PostPosted: Tue Jun 05, 2012 02:47
by Temperest
Currently, the Mesecons mod has pistons, but they don't properly update things like any other mesecon component. I'd like to see an addition to the Lua API that would allow us to simulate player digging/placing, so we can do things like improve pistons so they can properly push conductive stuff like mesecon.

Here's a possible interface:

minetest.env:place_node(pos,node)
minetest.env:dig_node(pos)


Or even better, expose all those property names in the node objects returned by get_node:

minetest.env:get_node(pos).after_place_node(pos)


Not sure how feasible the second one would be.

PostPosted: Tue Jun 05, 2012 04:43
by SegFault22
+1
Great idea! This might turn out to be something useful!

PostPosted: Tue Jun 05, 2012 05:23
by Stef
why not building some gravity pistons and other ones

PostPosted: Tue Jun 05, 2012 06:40
by darkrose
mesecons just needs to be changed to use the new on_construct and on_destruct callbacks, as (for instance) a piston pushing a mesecon out of a circuit will trigger on_destruct when it's taken out of the circuit and on_construct when it's placed in it's new position.

PostPosted: Tue Jun 05, 2012 06:52
by celeron55
Temperest wrote:Currently, the Mesecons mod has pistons, but they don't properly update things like any other mesecon component. I'd like to see an addition to the Lua API that would allow us to simulate player digging/placing, so we can do things like improve pistons so they can properly push conductive stuff like mesecon.


The problem here is, the on_place and on_dig callbacks require having an object which is doing the placement or digging, and just defining that it *rarely* can be nil isn't wise, because nobody will remember it and everything will crash.

There are a few additions in 0.4.dev-20120603 related to this:
- There now exist the on_construct and on_destruct callbacks to nodes, which are always called. They should now be responsible of setting up the node in such a way that it will be operable.
- You can now fully copy and set the metadata of a node, allowing moving of nodes with metadata.

The new furnace code uses this hack to replace the furnace node without destroying it's metadata:
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
function hacky_swap_node(pos,name)
    local node = minetest.env:get_node(pos)
    local meta = minetest.env:get_meta(pos)
    local meta0 = meta:to_table()
    if node.name == name then
        return
    end
    node.name = name
    local meta0 = meta:to_table()
    minetest.env:set_node(pos,node)
    meta = minetest.env:get_meta(pos)
    meta:from_table(meta0)
end


However, the on_construct and on_destruct callbacks don't fix everything. Item drops and other environmental effects should not be done in on_construct/on_destruct, because otherwise there is *no way* to skip them. And some mesecon and other things probably want to cause them.

Maybe there could be a "nobody" object that can be set as a parameter to everything as needed - that would solve many things. I'll see what I can come up with tonight.

PostPosted: Tue Jun 05, 2012 14:47
by Temperest As Guest
darkrose wrote:mesecons just needs to be changed to use the new on_construct and on_destruct callbacks, as (for instance) a piston pushing a mesecon out of a circuit will trigger on_destruct when it's taken out of the circuit and on_construct when it's placed in it's new position.


I've already updated the mesecons mod to use after_*_node functions, but the on_destruct functions will not work for this case because mesecons expects the node to have alredy been removed by the time the mesecons:receptor_off(pos) function is called. Additionally, this would create huge slowdowns when loading mesecon circuits using soemthing like WorldEdit (which I do rather frequently), and may even crash the game. Mesecons probably would need significan internal changes to support any other form.

New suggestion: something like after_destruct_node? It would alo solve a few other things I have in mind.

PostPosted: Tue Jun 05, 2012 15:14
by sfan5
Temperest As Guest wrote:
darkrose wrote:mesecons just needs to be changed to use the new on_construct and on_destruct callbacks, as (for instance) a piston pushing a mesecon out of a circuit will trigger on_destruct when it's taken out of the circuit and on_construct when it's placed in it's new position.


I've already updated the mesecons mod to use after_*_node functions, but the on_destruct functions will not work for this case because mesecons expects the node to have alredy been removed by the time the mesecons:receptor_off(pos) function is called. Additionally, this would create huge slowdowns when loading mesecon circuits using soemthing like WorldEdit (which I do rather frequently), and may even crash the game. Mesecons probably would need significan internal changes to support any other form.

New suggestion: something like after_destruct_node? It would alo solve a few other things I have in mind.

after_destruct_node is already in

PostPosted: Tue Jun 05, 2012 20:16
by celeron55
place_node, dig_node and punch_node:

https://github.com/celeron55/minetest/commit/c3658e7c797cbf5b9d04a6d950505d37dcdd422b

Please check out and tell if there are problems. These will be included in a development snapshot in a day or two.

PostPosted: Tue Jun 05, 2012 20:24
by celeron55
Temperest As Guest wrote:I've already updated the mesecons mod to use after_*_node functions, but the on_destruct functions will not work for this case because mesecons expects the node to have alredy been removed by the time the mesecons:receptor_off(pos) function is called. Additionally, this would create huge slowdowns when loading mesecon circuits using soemthing like WorldEdit (which I do rather frequently), and may even crash the game. Mesecons probably would need significan internal changes to support any other form.

New suggestion: something like after_destruct_node? It would alo solve a few other things I have in mind.


Is after_destruct really necessary? Is there any use for it other than easier support for old mesecons code?

PostPosted: Tue Jun 05, 2012 20:40
by Temperest
sfan5 wrote:after_destruct_node is already in


I believe you meant after_dig_node, since after_destruct_node is nowhere to be found in lua_api.txt. The latter is being proposed because it triggers in more cases than after_dig_node, such as pushing by pistons, removal by mods, or et cetera.

PostPosted: Tue Jun 05, 2012 20:41
by Temperest
In any case celeron55 has added this functionality as originally suggested. Thanks celeron55!

Edit: Mesecons will be updated to use this at the next dev build.

PostPosted: Tue Jun 05, 2012 20:53
by celeron55
I also added after_destruct, because adding it is very cheap and the point of the modding API is to not cause unnecessary burden on doing things.

https://github.com/celeron55/minetest/commit/3a0562bebcb91d05fceb5a1f9ded539f77a625e4