minetest-wiki-minenux/en/backporting/minetest-engine-api.md

63 lines
3.6 KiB
Markdown

### vector helpers
#### vector.offset
https://codeberg.org/minenux/minetest-mod-mobs_redo/issues/108
In particular, offsets are usually required for defining an area to pass to minetest.find_nodes_in_area() as well as the VoxelArea constructor. Likewise, an offset of y + 1 is frequently needed with functions like minetest.get_node() and minetest.get_node_light().
Here's are some concrete examples from minetest-mods where these functions would prove useful:
https://github.com/minetest-mods/loot/blob/e7398c3ce1953c443406392efd4fc1bd4d22e771/loot_vault.lua#L20
https://github.com/minetest-mods/mesecons/blob/7013f2e5d47ff2d21fb66fe82966a34ece947eec/mesecons_random/init.lua#L14
https://github.com/minetest-mods/painting/blob/8961849797fa21f168191b10b3693dd5eeab08ea/init.lua#L206
https://github.com/minetest-mods/schemlib_builder_npcf/blob/85cfb2d773a8d18ebd921f18f6bb7565664314e1/init.lua#L239
https://github.com/minetest-mods/mob-engine/blob/c2bedc3816a0c6a3a96b5803bd910b32c09c4f2c/creatures/functions.lua#L34
https://github.com/minetest-mods/mapgen_helper/blob/9c7347691d451f6eee329cd8f7e23ccbd27bd298/place_schematic.lua#L136
Note that vector.offset_y() isn't the direct analog of vector.offset(), because it defaults to 1. Checking the position directly above another position is a very common use-case In fact, nine out of ten situations where I need an offset position, it is y + 1.
vector.offset_y(pos)
vector.offset(pos, 0, 1, 0)
Keep in mind, the goal of these functions isn't to save keystrokes, it's to make code clearer to understand and easier maintain. By using vector.offset_y(), it is apparent at first glance what the intention is, thereby mitigating the likelihood of introducing bugs later, as could happen when using just vector.offset() where it is not as immediately obvious that x and z should never change.
Likewise, one could manually clamp a number by pairing math.min() with math.max(), but that is a far cry from the simplicity and clarity of math.clamp().
### reason or PlayerHPChangeReason
PlayerHPChangeReason table kind of, the minetest api is so crap that does not specified that has properties.. neither refers to the `PlayerHPChangeReason` object, the most important things here are:
> **since** 5.0
* `from` with values of
* `mod`
* `engine`
* `type` values when specific hp actions, and also any of those will have additional fields from mods.
* `set_hp`: A mod or the engine called set_hp without giving a type - use this for custom damage types.
* `punch`: Was punched. reason.object will hold the puncher, or nil if none.
* `fall`: the player dies from a high fall or something forced
* `node_damage`: `damage_per_second` from a neighboring node. for older 5.2 use the position of player to get `node_pos`
* `drown`: **since 5.5**
* `respawn`:
* `node` **since 5.3** will hold the node name or nil if `type` is `node_damage`.
* `node_pos` **since 5.3** will hold the position of the node if `type` is `node_damage`.
### register_on_dieplayer and register_on_player_hpchange
Those functions for player dead were enhanced since version 5, using new [reason or PlayerHPChangeReason](#reason-or-playerhpchangereason)
* `minetest.register_on_dieplayer(function(ObjectRef))` **since 0.4.15, 4.0**
* `minetest.register_on_dieplayer(function(ObjectRef, reason))` **since 5.0**
Those functions ofr player hp change where enhanced since version 5
* `minetest.register_on_player_hpchange(func(player, hp_change), modifier)` **since 0.4.15, 4.0**
* `minetest.register_on_player_hpchange(function(player, hp_change, reason), modifier)` **since 5.0**