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

3.6 KiB

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:

e7398c3ce1/loot_vault.lua (L20)

7013f2e5d4/mesecons_random/init.lua (L14)

8961849797/init.lua (L206)

85cfb2d773/init.lua (L239)

c2bedc3816/creatures/functions.lua (L34)

9c7347691d/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

  • 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