Simple Unknown Node(s) Replacer

Post Reply
User avatar
Hamlet
Member
Posts: 766
Joined: Sat Jul 29, 2017 21:09
IRC: H4mlet
In-game: Hamlet
Location: Lombardy, Italy

Simple Unknown Node(s) Replacer

by Hamlet » Post

Issue: removing a mod left around dozens of unknown nodes.
Idea: instead of manually deleting them, let the Engine do it.
How: using a simple LBM that will do it once that the map block is loaded, and never again.

What do I need to know?

- the removed mod name, e.g. "testmod"
- the unknown node(s) name(s).
-- digging an unknown node will display its name in the chat and/or console (0.4.16).

Unknown node's name, e.g. "testmod:redcube"

Now, since it's on the ground, I want it to be replaced with "default:dirt".

Code (CC0):

Code: Select all

minetest.register_lbm({
	name = ":lbm1",
	nodenames = {"testmod:redcube", "testmod:bluecube", "insert:namehere"},
	-- you can add as many entries you need
	action = function(pos)
		minetest.set_node(pos, {name = "default:dirt"}) -- replacer node name
	end,
})
Nice, but what if I have flying nodes that I want to replace with air?

Just add another LBM after the first (CC0):

Code: Select all

minetest.register_lbm({
	name = ":lbm2",
	nodenames = {"testmod:yellowcube", "testmod:pinkcube", "insert:namehere"},
	-- you can add as many entries you need
	action = function(pos)
		minetest.set_node(pos, {name = "air"}) -- replacer node name
	end,
})
And so on.

About the unknown nodes that might be in your inventory, trash them while in creative mode or open ..minetest/worlds/your_worlds_name/players/your_characters_name
CTRL+F ---> testmod:redcube ---> replace "Item testmod:redcube" with: Empty

Quick and dirty, but it worked for me; maybe it will do for you as well.

-- Edit
The original post was based on ABMs use, thanks to Naj for telling me about LBMs.
Last edited by Hamlet on Tue Sep 05, 2017 18:15, edited 1 time in total.
My repositories: Codeberg.org | My ContentDB's page

User avatar
Hamlet
Member
Posts: 766
Joined: Sat Jul 29, 2017 21:09
IRC: H4mlet
In-game: Hamlet
Location: Lombardy, Italy

Re: Simple Unknown Node(s) Replacer

by Hamlet » Post

Naj wrote:Good idea.

For such tasks, I'd rather use a LBM than an ABM (using minetest.register_lbm).

LBM is only triggered once per map block (and never launched again later on this mapblock). No need to worry about turning it off, it is a one shot.
Tried it, way better and CPU-saving; I've edited the first post accordingly, thanks for sharing the knowledge.
My repositories: Codeberg.org | My ContentDB's page

User avatar
AleksSyntek
Member
Posts: 30
Joined: Sat Jun 17, 2017 18:01
GitHub: AleksSyntek
IRC: Aleks
In-game: Aleks
Location: Argentina
Contact:

Re: Simple Unknown Node(s) Replacer

by AleksSyntek » Post

Good advice! I need to do some clean on various worlds that I have using some time ago and where I'd disabled some incompatible mods that did big mess on the terrain and manually tried to replace (so hard, boring and raging). because I know the mod name and node's names I wish to do this kind of massive clean/replace.
So in order to run this code we need to put into the init.lua file of the subgame right? Any guide to accomplish this will be appreciated!
Crash happens.

User avatar
Hamlet
Member
Posts: 766
Joined: Sat Jul 29, 2017 21:09
IRC: H4mlet
In-game: Hamlet
Location: Lombardy, Italy

Re: Simple Unknown Node(s) Replacer

by Hamlet » Post

AleksSyntek wrote:So in order to run this code we need to put into the init.lua file of the subgame right? Any guide to accomplish this will be appreciated!
Step by step:

..minetest/mods/

create a directory, example "replacer"

..minetest/mods/replacer/

create "init.lua"

paste the code into it, save and exit

run Minetest, activate the "replacer" mod for your map(s)

Or, you can also drop the "replacer" mod that you've just created into

../minetest/games/subgame_name/mods/

this way you will not need to activate it in the usual manner.
My repositories: Codeberg.org | My ContentDB's page

User avatar
AleksSyntek
Member
Posts: 30
Joined: Sat Jun 17, 2017 18:01
GitHub: AleksSyntek
IRC: Aleks
In-game: Aleks
Location: Argentina
Contact:

Re: Simple Unknown Node(s) Replacer

by AleksSyntek » Post

Thanks Hamlet!, I will try to activate it as a general mod then sit to see the magic happens :-P.
Crash happens.

FaceDeer
Member
Posts: 506
Joined: Sat Aug 29, 2015 19:01
GitHub: FaceDeer

Re: Simple Unknown Node(s) Replacer

by FaceDeer » Post

Here's an elaboration on this concept that I just whipped up:

Code: Select all

local nodenames_to_remove = {"testmod:bluenode", "testmod:rednode"}
local radius = 1

minetest.register_lbm({
	name = ":nodecleaner",
	nodenames = nodenames_to_remove,
	action = function(pos)
		local counts = {}
		for x= pos.x-radius, pos.x+radius, 1 do
			for y= pos.y-radius, pos.y+radius, 1 do
				for z = pos.z-radius, pos.z+radius, 1 do
					local testnode = minetest.get_node({x=x,y=y,z=z}).name
					counts[testnode] = (counts[testnode] or 0) + 1
				end
			end
		end
		
		local maxnode_name = "air" -- defaults to air if literally everything around it is nodes to remove
		local maxnode_count = 0
		for name, count in pairs(counts) do
			if count > maxnode_count then
				local valid_node = true
				for _, ignore in pairs(nodenames_to_remove) do
					if name == ignore then
						valid_node = false -- this is one of the node types we're removing, we don't want to add a new one of those
						break
					end
				end
				if valid_node then
					maxnode_name = name
					maxnode_count = count
				end
			end
		end
		minetest.set_node(pos, {name = maxnode_name}) -- replacer node name
	end,
})
I don't actually have Minetest or LUA installed on this computer at the moment so no guarantees that this even compiles, but I think the basic idea is sound and I'll fix it up a bit when I get back home tonight. :)

Basically what this does is, whenever there's an invalid node it looks at all of the nodes in a cube of radius 1 around it and counts up the various sorts of nodes that it finds. It then replaces it with the most common node that it found. So if testnode:rednode is embedded in stone it will be replaced with stone, and if it's floating in air it will be replaced with air, and so on. Nodes that are on the to-be-removed list are ignored. This should run pretty quickly and result in vaguely reasonable-looking replacements under most circumstances. Boost the "radius" value to make the lbm look farther, though I bet a radius of 1 should be fine under most circumstances.

I suppose it might theoretically be possible to abuse this to "replicate" valuable nodes (inventories are not replicated), but you'd have to know that something like this was going to happen ahead of time. Seems pretty unlikely and a lot of work for small reward.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests