[Mod / Library] Multinode Structure Matching [multiblock]

Post Reply
Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

[Mod / Library] Multinode Structure Matching [multiblock]

by Byakuren » Post

Github: https://github.com/raymoo/multiblock
API: https://github.com/raymoo/multiblock/bl ... er/API.txt
Depends: Nothing (default is required for the demo)
License: LGPL2 or any later version
Download: https://github.com/raymoo/multiblock/archive/master.zip

Multiblock is a pattern matching library for node structures. Suppose I want to make a node duplication altar. Then I can make a pattern like this:

Code: Select all

local pattern = {
        size = {x=5, y=2, z=5},
        center = {x=3, y=2, z=3},
        pattern = {
                "default:cobble", "default:cobble", "default:cobble", "default:cobble", "default:cobble",
                "default:cobble", "default:cobble", "default:cobble", "default:cobble", "default:cobble",
                "default:cobble", "default:cobble", "default:cobble", "default:cobble", "default:cobble",
                "default:cobble", "default:cobble", "default:cobble", "default:cobble", "default:cobble",
                "default:cobble", "default:cobble", "default:cobble", "default:cobble", "default:cobble",

                "?air", "?air", "?air", "?air", "?air",
                "?air", "mat!", "mat!", "mat!", "?air",
                "?air", "mat!", "multiblock:test_core", "mat!", "?air",
                "?air", "mat!", "mat!", "mat!", "?air",
                "?air", "?air", "?air", "?air", "?air",
        },
}
This specifies a structure with a 5x5 cobble base, and a layer on top with air on the outside edge, the chosen material surrounding the middle, and a "multiblock:test_core" in the center. The question mark before "air" makes that node pattern a "capture", which means the name and position of the node is saved for later use. The structure of an ordinary capture is "capturename?nodename". "capturename" names the capture (here it is empty, so the capture name is ""), and nodename is either the name of a node or a group specified with "group:groupname". "mat!" is a different kind of capture called a strict capture. Every node matched under a strict capture with the same name must be the same node. The structure of the pattern is the same as for normal captures - here I left nodename blank, so it will match any node. I use the strict capture to enforce that all eight of the nodes are the same node.

Later, I can invoke it like this:

Code: Select all

local function fill_out_possibly(pos, clicker)
        local captures = multiblock.match(pattern, pos)
        if not captures then
                minetest.chat_send_player(clicker:get_player_name(), "Match failed.")
                return
        end

        local mat = captures.mat[1]

        for i, capture in ipairs(captures[""]) do
                minetest.place_node(capture.pos, mat)
        end
end
I am able to access the nodes matched by the "mat" capture name in the corresponding field of the capture table. The unnamed air captures end up in the capture list for "" instead, and I use their positions to duplicate the nodes.

Trying the demo:
1. Give yourself a multiblock:test_core with /giveme
2. Place a 5x5 base of cobble
3. Place the test core on top, in the middle
4. Surround the core with 8 identical nodes of your choice
5. Right-click the core
6. Rejoice in your duplication hax

It is possible to use this mod as a standalone library. Copy multiblock.lua and do

Code: Select all

local multiblock = dofile(path_to_multiblock_lua)
Last edited by Byakuren on Mon Aug 01, 2016 19:46, edited 2 times in total.
Every time a mod API is left undocumented, a koala dies.

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: [Mod] Match Patterns on Multinode Structures [multiblock

by Byakuren » Post

I've separated out the actual functionality from init.lua, so it is easy to use as a non-mod library. Simply put multiblock.lua in your mod directory and call

Code: Select all

local multiblock = dofile(modpath .. "/multiblock.lua")
(Assuming you have bound modpath to your mod path)
Every time a mod API is left undocumented, a koala dies.

User avatar
SegFault22
Member
Posts: 872
Joined: Mon May 21, 2012 03:17
Location: NaN

Re: [Mod / Library] Multinode Structure Matching [multiblock

by SegFault22 » Post

This mod could be very useful, as it enables other mods to add multiblock structures, which perform special functions - such as a coal coke oven, blast furnace, smelter, turbines, factory machines, nuclear reactor, and much more. You could even use this for building vehicles, where several different nodes containing specific parts are put together and when all are present, the vehicle-entity forms (taking the nodes with it, of course). The possibilities are pretty much endless.

But, there appears to be a problem, at least on my system - whenever I rightclick the multiblock:test_core when it is part of the structure, and the blocks surrounding it use facedir, I get an error "attempt to index local 'dir' (a nil value)", which is either caused by something in the mod or something in the builtin item.lua... here's all the stuff it puts out:

Code: Select all

2016-09-29 08:35:55: ERROR[Main]: ServerError: Lua: Runtime error from mod 'default' in callback item_OnPlace(): Runtime error from mod 'default' in callback item_OnPlace(): /usr/share/minetest/builtin/game/item.lua:76: attempt to index local 'dir' (a nil value)
2016-09-29 08:35:55: ERROR[Main]: stack traceback:
2016-09-29 08:35:55: ERROR[Main]: 	/usr/share/minetest/builtin/game/item.lua:76: in function 'dir_to_facedir'
2016-09-29 08:35:55: ERROR[Main]: 	/usr/share/minetest/builtin/common/misc_helpers.lua:386: in function 'rotate_and_place'
2016-09-29 08:35:55: ERROR[Main]: 	/usr/share/minetest/builtin/common/misc_helpers.lua:465: in function </usr/share/minetest/builtin/common/misc_helpers.lua:464>
2016-09-29 08:35:55: ERROR[Main]: 	[C]: in function 'place_node'
2016-09-29 08:35:55: ERROR[Main]: 	/home/segfault22/.minetest/mods/multiblock/test.lua:35: in function 'on_rightclick'
2016-09-29 08:35:55: ERROR[Main]: 	/usr/share/minetest/builtin/game/item.lua:333: in function </usr/share/minetest/builtin/game/item.lua:326>
2016-09-29 08:35:55: ERROR[Main]: stack traceback:
2016-09-29 08:35:55: ERROR[Main]: 	[C]: in function 'place_node'
2016-09-29 08:35:55: ERROR[Main]: 	/home/segfault22/.minetest/mods/multiblock/test.lua:35: in function 'on_rightclick'
2016-09-29 08:35:55: ERROR[Main]: 	/usr/share/minetest/builtin/game/item.lua:333: in function </usr/share/minetest/builtin/game/item.lua:326>
It isn't necessary that blocks with facedir can be part of the structure, but it would be useful in some cases, such as with the smelter (drain facing outwards pulls liquid from the smelter, facing inwards puts liquid into it; or the direction of the smelter controller being important due to the "instruments" being all on one side and the "probes" on the other)

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: [Mod / Library] Multinode Structure Matching [multiblock

by Byakuren » Post

The crash I would consider a bug with the test script, but it's true that the mod could have better support for facedir. I might as well just make captures be the standard node table format + position field, which would fix this. I won't have time for a few hours, though.
Every time a mod API is left undocumented, a koala dies.

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: [Mod / Library] Multinode Structure Matching [multiblock

by Byakuren » Post

Ok, just updated it so that captures include node params (and thus can be used like a node argument). I've tested it and it appears to work with both torches and stairs, which I am pretty sure use param2 for facedir.
Every time a mod API is left undocumented, a koala dies.

User avatar
SegFault22
Member
Posts: 872
Joined: Mon May 21, 2012 03:17
Location: NaN

Re: [Mod / Library] Multinode Structure Matching [multiblock

by SegFault22 » Post

Byakuren wrote:Ok, just updated it so that captures include node params (and thus can be used like a node argument). I've tested it and it appears to work with both torches and stairs, which I am pretty sure use param2 for facedir.
Thank you for doing all of this work to make such a great mod. I will use it to make lots of cool stuff, which will increase the immersion-factor of the game immensely.

In the past I was considering making a mod for a multiblock coal coke oven and other things, but I decided to postpone that because I thought I would have to make a system "from scratch" to handle the multiblock structures. Now that you have made this system, it's almost trivial to implement that coal coke oven and much more.

User avatar
texmex
Member
Posts: 1753
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: tacotexmex

Re: [Mod / Library] Multinode Structure Matching [multiblock

by texmex » Post

Are there any mods built with this?

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests