ABMs and booleans

Post Reply
DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

ABMs and booleans

by DonCoronado » Post

Disclaimer : I'm absolutely new at this.

Is there a way of introducing boolean conditions in order for things to change? Not just temporal conditions based on chance, but boolean conditions that may trigger some result.

I've tried to do it introducing the if-then-else structure inside an ABM, as follows:

Code: Select all

booleano = 1

	minetest.register_abm({ 
		nodenames = {"default:river_water_source", "default:river_water_flowing"},
		interval = 5.0, -- Run every 5 seconds
		chance = 2, -- Select every 1 in 2 nodes

		action = function(pos, node, active_object_count, active_object_count_wider)
			if(booleano == 1)
			then
				minetest.set_node({x = pos.x, y = pos.y, z = pos.z}, {name = "default:dirt"})
				booleano = 0
			else
				minetest.set_node({x = pos.x, y = pos.y, z = pos.z}, {name = "default:stone_with_gold"})
				booleano = 1
			end
		end

	})
but unsurprisingly it didn't work.

User avatar
duane
Member
Posts: 1715
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: ABMs and booleans

by duane » Post

What didn't work? I'm not sure what you wanted your code to do or what happened.

You do know that the variable you're using will be shared by all the river nodes, right? It's not going to be unique to each node.
Believe in people and you don't need to believe anything else.

User avatar
TenPlus1
Member
Posts: 3715
Joined: Mon Jul 29, 2013 13:38
In-game: TenPlus1
Contact:

Re: ABMs and booleans

by TenPlus1 » Post

What are you trying to do exactly, if we know that then we can help more ?

DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

Re: ABMs and booleans

by DonCoronado » Post

This was just a test. The idea was to know whether a river water node could be converted, alternatively, in gold or in dirt. Just to know whether the if-then-else statement worked. I mean, I am not interested in this function in particular, but in the general form of booleans in ABMs.

User avatar
duane
Member
Posts: 1715
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: ABMs and booleans

by duane » Post

DonCoronado wrote:
Fri Jan 22, 2021 11:09
This was just a test. The idea was to know whether a river water node could be converted, alternatively, in gold or in dirt. Just to know whether the if-then-else statement worked. I mean, I am not interested in this function in particular, but in the general form of booleans in ABMs.
Ok, how did you attempt to verify the code? What happened? What did you expect to happen? You'll have to be more specific to get a useful response. It also looks as though you didn't post all of your code. The parts you left out might be a problem as well, so attach/link the whole file if possible. I can pretty much guarantee that the if-then-else worked; it just didn't do what you wanted.

Edit: To answer your question much more generally -- yes, you can use boolean conditions in abms, just as you can use them anywhere else. Any variable will be just as valid, provided it doesn't fall out of scope.

A variable scoped outside of the abm will be shared by all invocations of the abm's action. You can introduce a local variable inside the action, but it is not static. If you want information to be retained locally to the node, between calls of the action, you must store metadata in the node and retrieve it in the action. (Or, I suppose you could just store it in a big table, for each node, if you don't mind it disappearing when the server shuts down.)
Believe in people and you don't need to believe anything else.

DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

Re: ABMs and booleans

by DonCoronado » Post

Hey! Sorry for the late reply.
So, what I thought would happen is that after the first 5 seconds, ~half of the river water blocks would turn into dirt (booleano=1), and then after 5 seconds, ~a quarter of the river water blocks would turn into gold (booleano=0), and then after 5 seconds, ~an eight of the blocks into dirt (booleano=1), and so on and so forth. But no block was changed.

User avatar
duane
Member
Posts: 1715
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: ABMs and booleans

by duane » Post

DonCoronado wrote:
Mon Jan 25, 2021 20:46
Hey! Sorry for the late reply.
So, what I thought would happen is that after the first 5 seconds, ~half of the river water blocks would turn into dirt (booleano=1), and then after 5 seconds, ~a quarter of the river water blocks would turn into gold (booleano=0), and then after 5 seconds, ~an eight of the blocks into dirt (booleano=1), and so on and so forth. But no block was changed.
Well, that won't happen given the code you've shown, since the variable will be changed every time your action runs. (See my previous posts.)

However, if nothing happens then your abm may not be running at all. Make sure you aren't looking at regular water blocks instead of river water. Check your log for errors. Put a print statement in, to be sure it runs. You might also post the rest of the code if you want someone to check it.
Believe in people and you don't need to believe anything else.

DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

Re: ABMs and booleans

by DonCoronado » Post

It now works, it seems I had troubles loading the mod. However, instead of iterating between dirt and gold every five seconds, it just randomly turns some nodes into gold and some other into dirt.

DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

Re: ABMs and booleans

by DonCoronado » Post

Let us see now this piece of code, more specific. The idea is to replicate the behaviour of a river flood:

Code: Select all

-- floods
flood = 1
minetest.register_abm({ 
	nodenames = {"default:river_water_source"},
	neighbors = {"air"},
	interval = 5.0, -- Run every 5 seconds
	chance = 1, -- Select every 1 in 1 nodes

	action = function(pos, node, active_object_count, active_object_count_wider)
		if(flood == 1)
		then
			minetest.set_node({x = pos.x, y = pos.y+1, z = pos.z}, {name = "default:river_water_source"})
			flood = 0
		else
			if(flood == -1)
			then
				minetest.set_node({x = pos.x, y = pos.y, z = pos.z}, {name = "air"})
				flood = 1
			end
		end
	end
})
The idea is that, if flood == 1, then all the river water nodes in contact with air (ie, the superficial river water nodes) will have, five seconds later, a river water node on them. But it will happen only once, hence the boolean flood is set to 0.
Now, even though I have specified the

Code: Select all

chance = 1
parameter, meaning that the probability of it happening is 1 (isn't it?), it only happens to ONE node, not nearly all.

User avatar
duane
Member
Posts: 1715
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: ABMs and booleans

by duane » Post

I get the feeling that you're expecting the action to run once every five seconds, but that is not the case. It runs once for every node that meets the listed requirements, every five seconds (on average).

So, if there are fifty nodes near you, it will run fifty times (or less) every five seconds.
Believe in people and you don't need to believe anything else.

DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

Re: ABMs and booleans

by DonCoronado » Post

I see now, thank you a lot! Now I'm stuck with the problem of how to develop a flood-like behaviour, in which the superficial nodes of water generate one node on them only once.

User avatar
duane
Member
Posts: 1715
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: ABMs and booleans

by duane » Post

DonCoronado wrote:
Wed Jan 27, 2021 15:24
I see now, thank you a lot! Now I'm stuck with the problem of how to develop a flood-like behaviour, in which the superficial nodes of water generate one node on them only once.
The easiest way would be just to have two different identical water nodes, and change the node type after it generates. However, it might be more efficient to store the state as metadata.
Believe in people and you don't need to believe anything else.

User avatar
sorcerykid
Member
Posts: 1841
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: ABMs and booleans

by sorcerykid » Post

Correct me if I'm wrong, but I believe you could use param2 for this purpose. It would be lot more efficient than metadata.

User avatar
duane
Member
Posts: 1715
Joined: Wed Aug 19, 2015 19:11
GitHub: duane-r
Location: Oklahoma City
Contact:

Re: ABMs and booleans

by duane » Post

sorcerykid wrote:
Thu Jan 28, 2021 17:46
Correct me if I'm wrong, but I believe you could use param2 for this purpose. It would be lot more efficient than metadata.
Would it be more efficient? I'm not sure. If it's affecting all regular water nodes, efficiency would be important, but if it only happened with rivers or rarely with water, I wouldn't worry.

Mostly, I'd be afraid some engine function or someone else's mod would overwrite it. Metadata can be fairly unique.
Believe in people and you don't need to believe anything else.

User avatar
v-rob
Developer
Posts: 970
Joined: Thu Mar 24, 2016 03:19
GitHub: v-rob
IRC: v-rob
Location: Right behind you.

Re: ABMs and booleans

by v-rob » Post

I think flowing water uses param2 for how it looks, although I may be wrong. It would be more efficient, but definitely less reliable in terms of mod compatibility.
Core Developer | My Best Mods: Bridger - Slats - Stained Glass

DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

Re: ABMs and booleans

by DonCoronado » Post

Alternatively, what about using a "dull" river node that is only generated when there is a flood? You have the regular river node, then you have the flood node on it (indistinguishable or not from the river node), and then it's gone. It is not elegant, but I think it should work.

Edit: it seems to work!
Last edited by DonCoronado on Sat Jan 30, 2021 20:18, edited 1 time in total.

DonCoronado
Member
Posts: 12
Joined: Sun Sep 27, 2020 16:47
GitHub: tomasmcoronado

Re: ABMs and booleans

by DonCoronado » Post

Do you know how could I create a floods:flood_water_flowing node? Because in order to make my flood water flow, I need to use default:water_flowing node.

Edit: I think I found how!

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests