minetest_modding_book/_en/map/abms.md

78 lines
2.6 KiB
Markdown
Raw Normal View History

2014-12-15 23:04:40 +03:00
---
title: Active Block Modifiers
layout: default
2018-07-15 21:36:35 +03:00
root: ../..
2018-07-15 17:28:10 +03:00
idx: 3.2
description: Learn how to make ABMs to change blocks.
2018-07-15 21:13:16 +03:00
redirect_from: /en/chapters/abms.html
2014-12-15 23:04:40 +03:00
---
2015-02-22 13:28:37 +03:00
## Introduction
2014-12-15 23:04:40 +03:00
2018-10-04 22:23:54 +03:00
An Active Block Modifier (ABM) is a method of periodically running a
2018-10-20 03:37:41 +03:00
function on nodes matching specific criteria.
2018-10-04 22:23:54 +03:00
As the name implies, this only works on loaded MapBlocks.
2018-10-04 22:23:54 +03:00
ABMs are best suited for nodes which are frequently found in the world,
such as grass.
ABMs have a high CPU overhead, as Minetest needs to scan all Active Blocks
to find matching nodes, but they have a low memory and storage overhead.
2014-12-15 23:04:40 +03:00
2018-10-04 22:23:54 +03:00
For nodes which are uncommon or already use metadata, such as furnaces
and machines, node timers should be used instead.
2018-10-20 03:37:41 +03:00
Node timers don't involve searching all loaded nodes to find matches,
but instead require slightly more memory and storage for the tracking
of running timers.
2018-10-04 22:23:54 +03:00
* [Registering an ABM](#registering-an-abm)
* [Your Turn](#your-turn)
2018-10-04 22:23:54 +03:00
## Registering an ABM
2014-12-15 23:04:40 +03:00
Alien grass, for the purposes of this chapter, is a type of grass which
has a chance to appear near water.
2014-12-15 23:04:40 +03:00
```lua
2014-12-15 23:04:40 +03:00
minetest.register_node("aliens:grass", {
description = "Alien Grass",
2018-09-24 19:16:00 +03:00
light_source = 3, -- The node radiates light. Min 0, max 14
tiles = {"aliens_grass.png"},
groups = {choppy=1},
on_use = minetest.item_eat(20)
2014-12-15 23:04:40 +03:00
})
minetest.register_abm({
nodenames = {"default:dirt_with_grass"},
neighbors = {"default:water_source", "default:water_flowing"},
interval = 10.0, -- Run every 10 seconds
chance = 50, -- Select every 1 in 50 nodes
2018-09-24 19:16:00 +03:00
action = function(pos, node, active_object_count,
active_object_count_wider)
local pos = {x = pos.x, y = pos.y + 1, z = pos.z}
minetest.set_node(pos, {name = "aliens:grass"})
end
2014-12-15 23:04:40 +03:00
})
```
2014-12-15 23:04:40 +03:00
2018-10-20 03:37:41 +03:00
This ABM runs every ten seconds, and for each matching node there is
a 1 in 50 chance of it running.
2018-10-04 22:23:54 +03:00
If the ABM runs on a node, an alien grass node is placed above it.
Please be warned, this will delete any node previously located in that position.
To prevent this you should include a check using minetest.get_node to make sure there is space for the grass.
2014-12-15 23:04:40 +03:00
2018-10-04 22:23:54 +03:00
Specifying a neighbour is optional.
If you specify multiple neighbours, only one of them needs to be
present to meet the requirements.
2018-10-04 22:23:54 +03:00
Specifying chance is also optional.
If you don't specify the chance, the ABM will always run when the other conditions are met.
2014-12-15 23:04:40 +03:00
2015-02-22 13:28:37 +03:00
## Your Turn
2014-12-15 23:04:40 +03:00
2018-10-04 22:23:54 +03:00
* Midas touch: Make water turn to gold blocks with a 1 in 100 chance, every 5 seconds.
* Decay: Make wood turn into dirt when water is a neighbour.
* Burnin': Make every air node catch on fire. (Tip: "air" and "fire:basic_flame").
2014-12-15 23:04:40 +03:00
Warning: expect the game to crash.